简体   繁体   English

动作栏中项目标题的动态更改

[英]Dynamic change of items title in Action Bar

What I am trying to do is: change the title of an item in Action Bar from onClick method. 我想要做的是:从onClick方法更改Action Bar中项目的标题。 Code of onCreateOptionsMenu : onCreateOptionsMenu代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.option_menu, menu);    
    return super.onCreateOptionsMenu(menu);
}

My option_menu.xml of menu: 我的菜单的option_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/state"
      android:title="@string/title_not_connected"
      android:showAsAction="ifRoom" />
</menu>

The title should change from onClick method in class below: 标题应该从下面的类中的onClick方法更改:

@SuppressLint("ValidFragment")
public class ReceiveSectionFragment extends Fragment {
        public NotificationCenter mNotificationCenter;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            final View rootView = inflater.inflate(R.layout.receive, container, false);
            mNotificationCenter = new NotificationCenter();
            rootView.findViewById(R.id.bt_server_start)
            .setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    MenuItem item = (MenuItem) menu.findItem(R.id.state);
                    item.setTitle(TAG);
                }
            });
    }
}

As you can see I am trying to find this item in menu by Id but I am getting this error: The method findItem(int) is undefined for the type R.menu . 正如你所看到我试图在菜单中通过Id找到这个项目,但我收到此错误: The method findItem(int) is undefined for the type R.menu I am using fragments and ReceiveSectionFragment uses R.layout.receive layout. 我正在使用片段,而ReceiveSectionFragment使用R.layout.receive布局。

How can I deal with this problem? 我该如何处理这个问题? Possibly, I am doing it all wrong by using wrong methods. 可能,我使用错误的方法做错了。

I am not using ActionBar Sherlock. 我没有使用ActionBar Sherlock。

You have to call InvalidateOptionsMenu when you want to make changes to the ActionBar menu. 如果要对ActionBar菜单进行更改,则必须调用InvalidateOptionsMenu

You then override onCreateOptionsMenu to rebuild your menu with your changes in place. 然后,您可以覆盖onCreateOptionsMenu以便根据您的更改重建菜单。 You cannot edit a menu outside of the methods provided for building menus. 您无法在为构建菜单提供的方法之外编辑菜单。

Every time you call InvalidateOptionsMenu your menu will be rebuilt by the onCreateOptionsMenu method so you need to make all of your changes there. 每次调用InvalidateOptionsMenu您的菜单都将由onCreateOptionsMenu方法重建,因此您需要在那里进行所有更改。

http://developer.android.com/reference/android/app/Activity.html#invalidateOptionsMenu() http://developer.android.com/reference/android/app/Activity.html#invalidateOptionsMenu()

http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu(android.view.Menu) http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu(android.view.Menu)

This is true whether you are using ActionBarSherlock or not. 无论您是否使用ActionBarSherlock,都是如此。

eg: 例如:

Set a variable to hold the title 设置变量以保存标题

String MyMenuTitle = "Default Menu";

Your OnClick 你的OnClick

public void onClick(View view) {
    MyMenuTitle = "My New Menu Title";
}

Your Menu Building Code. 您的菜单构建代码。

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.option_menu, menu); 

    MenuItem item = menu.findItem(R.id.state);
    item.setTitle(MyMenuTitle);

    return super.onCreateOptionsMenu(menu);
}

You should insert the following lines in the fragment in which you want to hide the menu. 您应该在要隐藏菜单的片段中插入以下行。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);

    MenuItem item = (MenuItem) menu.findItem(R.id.your_action);
    item.setVisible(false);
}

There is another way to do this without InvalidateOptionsMenu.. 没有InvalidateOptionsMenu还有另一种方法可以做到这一点。

first declare the MenuItem as static before the onCreate Method.. 首先在onCreate方法之前将MenuItem声明为静态..

  static MenuItem item;

Then initialize handler in onCreateOptionsMenu 然后在onCreateOptionsMenu中初始化处理程序

  @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    item = menu.findItem(R.id.state);

    return true;
}

Finally use setTitle whenever you want to change your title... 最后每当你想改变你的标题时都使用setTitle ......

 item.setTitle("your title");

I hope this helps... 我希望这有帮助...

Its Working 它的工作

static MenuItem item; static MenuItem项; // Define Global Variable //定义全局变量

 @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        inflater.inflate(R.menu.minu_filter, menu);
        menu.findItem(action_enter_manually).setVisible(true);
        // allocation id 
        item = menu.findItem(R.id.action_enter_manually);
    }

call Where item.setTitle("your title"); call where item.setTitle(“your title”);

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

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