简体   繁体   English

TabActivity中用于特定选项卡的选项菜单

[英]Optionsmenu in an TabActivity for a specific Tab

My problem is, that I have got a TabActivity, which has 4 Tabs right now. 我的问题是,我有一个TabActivity,它现在有4个选项卡。 The first Tab is a special Details-Tab, where the user could modify some data. 第一个选项卡是特殊的“详细信息”选项卡,用户可以在其中修改某些数据。

The problem is, that if I add a OptionsMenu for the Activity, that the OptionsMenu is appearing on every Tab. 问题是,如果我为活动添加一个OptionsMenu,则OptionsMenu出现在每个选项卡上。 I tried to check the current mTabHost.getCurrentTabTag() in the onCreateOptionsMenu but that changed nothing. 我试图在onCreateOptionsMenu中检查当前的mTabHost.getCurrentTabTag() ,但没有任何改变。 So, how to do that? 那么,该怎么做呢?

(The following code, which still shows the OptionsMenu on every Tab) (以下代码,仍在每个选项卡上显示OptionsMenu)

 public boolean onCreateOptionsMenu(Menu menu) {    
  if(mTabHost.getCurrentTabTag()==getString(R.string.tab_details)) {
   boolean result = super.onCreateOptionsMenu(menu);
   menu.add(0, EDIT_ID, 0, R.string.menu_edit).setIcon(R.drawable.edit);
   return result;
  }    

  return true;
 }

 public boolean onMenuItemSelected(int featureId, MenuItem item) {
  switch (item.getItemId()) {
       case EDIT_ID: {   
        Toast.makeText(this, "o.O", Toast.LENGTH_LONG).show();   
       }
  }

  return super.onMenuItemSelected(featureId, item);
 } 

I see that you are comparing strings (tags in fact) with ==, usually, this is nicer to do that with .equals() method. 我看到您正在将==的字符串(实际上是标签)进行比较,通常,使用.equals()方法比较好。 Maybe it will solve your issue. 也许它将解决您的问题。
To dynamically update an option menu (from the documentation) : 要动态更新选项菜单(来自文档):

If you want to change the Options Menu each time it opens, you must override the onPrepareOptionsMenu() 如果要在每次打开“选项菜单”时进行更改,则必须重写onPrepareOptionsMenu()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM