Android 如何获取我的活动的选项菜单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10579333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the options menu of my Activity?
提问by Bobs
In some methods of my Activity I want to check the title of menu or know if it is checked or not. How can I get Activity's menu. I need something like this.getMenu()
在我的活动的某些方法中,我想检查菜单的标题或知道它是否被选中。我怎样才能获得活动的菜单。我需要类似的东西this.getMenu()
回答by Dustin
Be wary of invalidateOptionsMenu(). It recreates the entire menu. This has a lot of overhead and will reset embedded components like the SearchView. It took me quite a while to track down why my SearchViewwould "randomly" close.
警惕invalidateOptionsMenu(). 它重新创建了整个菜单。这有很多开销,并且会重置嵌入式组件,如SearchView. 我花了很长时间才弄清楚为什么我SearchView会“随机”关闭。
I ended up capturing the menu as posted by Dark and then call onPrepareOptionsMenu(Menu)as necessary. This met my requirement without an nasty side effects. Gotcha: Make sure to do a null check in case you call onPrepareOptionsMenu()before the menu is created. I did this as below:
我最终捕获了 Dark 发布的菜单,然后onPrepareOptionsMenu(Menu)根据需要调用。这满足了我的要求,没有令人讨厌的副作用。问题:确保在onPrepareOptionsMenu()创建菜单之前调用空值检查。我这样做如下:
private Menu mOptionsMenu;
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
mOptionsMenu = menu
...
}
private void updateOptionsMenu() {
if (mOptionsMenu != null) {
onPrepareOptionsMenu(mOptionsMenu);
}
}
回答by Diwakar Bhatia
Call invalidateOptionsMenu() instead of passing menu object around.
调用 invalidateOptionsMenu() 而不是传递菜单对象。
回答by Dark
you could do it by passing the Menu object to your Activity class
您可以通过将 Menu 对象传递给您的 Activity 类来实现
public class MainActivity extends Activity
{
...
...
private Menu _menu = null;
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
_menu = menu;
return true;
}
private Menu getMenu()
{
//use it like this
return _menu;
}
}
回答by Ostkontentitan
There several callback methods that provide menu as a parameter.
有几种回调方法提供菜单作为参数。
You might wanna manipulate it there.
你可能想在那里操纵它。
For example:
例如:
onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
onCreateOptionsMenu(Menu menu)
onCreatePanelMenu(int featureId, Menu menu)
There several more, best you take a look in activity documentation and look for your desired method: http://developer.android.com/reference/android/app/Activity.html
还有更多,最好查看活动文档并寻找所需的方法:http: //developer.android.com/reference/android/app/Activity.html
回答by Vineet Shukla
As far as I could understand what you want here is which may help you:
据我所知,您在这里想要的是它可以帮助您:
1.Refer this tutorial over option menu.
1.通过选项菜单参考本教程。
2.Every time user presses menu button you can check it's title thru getTitle().
2.每次用户按下菜单按钮时,您都可以通过 来查看它的标题getTitle()。
3.Or if you want to know the last menu item checked or selected when user has not pressed the menu button then you need to store the preferences when user presses.
3.或者,如果您想知道在用户没有按下菜单按钮时检查或选择的最后一个菜单项,那么您需要在用户按下时存储首选项。

