带菜单项的标签栏应用程序

时间:2015-10-29 08:39:31

标签: android

我正在使用标签栏处理Android应用程序,我想将项目添加到菜单栏,但是当我单击该项目时,它调用MainActivity上的onOptionsItemSelected而不是TabActivity。我该如何解决这个问题?

这是MainActivity代码


    package com.example.TabApplication;

    import android.app.TabActivity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.TabHost;
    import android.widget.TabHost.OnTabChangeListener;
    import android.widget.Toast;

    public class MainActivity extends TabActivity implements OnTabChangeListener {

             android.widget.TabHost tabHost;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             // Get TabHost Refference
             tabHost = getTabHost();

             // Set TabChangeListener called when tab changed
             tabHost.setOnTabChangedListener(this);

             TabHost.TabSpec spec;
             Intent intent;

              /************* TAB1 ************/
             intent = new Intent().setClass(this, FirstActivity.class);
             spec = tabHost.newTabSpec("First").setIndicator("tab1")
                           .setContent(intent);
             tabHost.addTab(spec);

             /************* TAB2 ************/
             intent = new Intent().setClass(this, SecondActivity.class);
             spec = tabHost.newTabSpec("Second").setIndicator("tab2")
                           .setContent(intent);  
             tabHost.addTab(spec);
             /************* TAB3 ************/
             intent = new Intent().setClass(this, ThirdActivity.class);
             spec = tabHost.newTabSpec("Third").setIndicator("tab3")
                           .setContent(intent);  
             tabHost.addTab(spec);
             /************* TAB4 ************/
             intent = new Intent().setClass(this, FourthActivity.class);
             spec = tabHost.newTabSpec("Fourth").setIndicator("tab4")
                           .setContent(intent);  
             tabHost.addTab(spec);
             /************* TAB5 ************/
             intent = new Intent().setClass(this, FifthActivity.class);
             spec = tabHost.newTabSpec("Fifth").setIndicator("tab5")
                           .setContent(intent);  
             tabHost.addTab(spec);

        }
        @Override
        public void onTabChanged(String tabId) {
            // TODO Auto-generated method stub

        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main,menu);
            return true;
        }
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.action_add_task:

                return true;

            default:
                return false;
            }
        }

这是FirstActivity代码


    package com.example.TabApplication;

    import mydear.db.TaskContract;
    import mydear.db.TaskDBHelper;
    import android.app.Activity;
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.widget.RadioButton;
    import android.widget.Toast;
    import android.widget.ViewFlipper;

    public class FirstActivity extends Activity {
        private TaskDBHelper helper;
        ViewFlipper vf;
        ListView lvItems;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_first);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.action_add_task:

                //Do somthing
                return true;

            default:
                return false;
            }
        }
        private void Display() {
            helper = new TaskDBHelper(FirstActivity.this);
            SQLiteDatabase sqlDB = helper.getReadableDatabase();
            String[] pro={
                    "_id",
                    "name",
                    "date"
            };
            Cursor cursor = sqlDB.query(TaskContract.TABLE,
                    pro,
                    null, null, null, null, null);
            TodoCursorAdapter todoAdapter = new TodoCursorAdapter(this, cursor,0);
            lvItems.setAdapter(todoAdapter);
        }
    }

2 个答案:

答案 0 :(得分:0)

在TabActivity中使用 setOnTabChangedListener

   static TabHost tabHost;
   tabHost = getTabHost();
   tabHost.setOnTabChangedListener(new OnTabChangeListener() {
   @Override
   public void onTabChanged(String arg0) {
   //your code
   }     
   });

答案 1 :(得分:0)

为菜单创建单独的xml,并为xml文件指定正确的tools:context值。例如,这是MainActivity的main.xml菜单。

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.catalog.MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
    android:orderInCategory="100" android:showAsAction="never" />

更改单独的xml菜单中的tools:context,如下所示

tools:context="com.example.catalog.YourCallingActivity"