带菜单项的标签栏应用程序
我正在使用标签栏的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); } }
在TabActivity中使用setOnTabChangedListener 。
static TabHost tabHost;
tabHost = getTabHost();
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String arg0) {
//your code
}
});
为菜单创建一个单独的xml,并将适当的tools:context
值分配给xml文件。 例如,这是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"
链接地址: http://www.djcxy.com/p/26227.html