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()
在我的Activity的一些方法中,我想检查菜单的标题或知道它是否被选中。我怎样才能获得Activity的菜单。我需要像这样的东西.getMenu()
5 个解决方案
#1
98
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 SearchView
would "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(菜单)。这符合我的要求,没有令人讨厌的副作用。问题:如果在创建菜单之前调用onPrepareOptionsMenu(),请务必进行空检查。我这样做如下:
private Menu mOptionsMenu;
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
mOptionsMenu = menu
...
}
private void updateOptionsMenu() {
if (mOptionsMenu != null) {
onPrepareOptionsMenu(mOptionsMenu);
}
}
#2
29
Call invalidateOptionsMenu() instead of passing menu object around.
调用invalidateOptionsMenu()而不是传递菜单对象。
#3
11
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;
}
}
#4
1
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
#5
1
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.或者,如果您想知道用户未按下菜单按钮时选中或选择的最后一个菜单项,则需要在用户按下时存储首选项。
#1
98
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 SearchView
would "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(菜单)。这符合我的要求,没有令人讨厌的副作用。问题:如果在创建菜单之前调用onPrepareOptionsMenu(),请务必进行空检查。我这样做如下:
private Menu mOptionsMenu;
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
mOptionsMenu = menu
...
}
private void updateOptionsMenu() {
if (mOptionsMenu != null) {
onPrepareOptionsMenu(mOptionsMenu);
}
}
#2
29
Call invalidateOptionsMenu() instead of passing menu object around.
调用invalidateOptionsMenu()而不是传递菜单对象。
#3
11
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;
}
}
#4
1
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
#5
1
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.或者,如果您想知道用户未按下菜单按钮时选中或选择的最后一个菜单项,则需要在用户按下时存储首选项。