Tab bar application with Menu Item

I am working on an android application with tab bar and I want to add item to the menu bar, but when I clicked the item it calls to the onOptionsItemSelected on the MainActivity instead of the TabActivity. How can I fix that?

This is the MainActivity code


    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;
            }
        }

This is the FirstActivity code


    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);
        }
    }


在TabActivity中使用setOnTabChangedListener

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

Create a separate xml for menu and assign the proper tools:context value to xml File. For example this is your main.xml Menu for MainActivity .

<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" />

Change the tools:context in your seperate xml menu like this

tools:context="com.example.catalog.YourCallingActivity"
链接地址: http://www.djcxy.com/p/26228.html

上一篇: 帮助ListView透明度

下一篇: 带菜单项的标签栏应用程序