Android - 通过代码启用菜单项
当上一个屏幕(活动)返回时,我需要启用菜单项。
我尝试了这段代码:
... ((MenuItem)findViewById(R.id.menu_how)).setEnabled(true); ...
但是引发了空指针异常。
顺便说一句,menu_how 在 xml 中设置为 false;该代码是 onActivityResult(int requestCode, int resultCode, Intent data) 调用的一部分。
I need to enable a MenuItem when a previous screen (Activity) returns.
I tried this code:
...
((MenuItem)findViewById(R.id.menu_how)).setEnabled(true);
...
but a null pointer exception is launched.
BTW, the menu_how is set to false in xml; and the code is part of onActivityResult(int requestCode, int resultCode, Intent data) call.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你在哪里调用这个?(抱歉,没有仔细阅读)我认为你需要在菜单膨胀后调用它(通常在OnCreateOptionsMenu中)。为此,您可以在其他 Activity 返回时将变量设置为 true,然后在调用后在 OnCreateOptionsMenu 中执行((MenuItem)findViewById(R.id.menu_how)).setEnabled(mMyBooleanField)
充气机.充气。编辑:要在代码中完成此操作,它可能看起来像这样:
在类的顶部(以及所有其他类成员):
Boolean mEnableMenuItem = false;
在 OnCreateOptionsMenu 中:
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_main, 菜单);
((MenuItem)findViewById(R.id.menu_how)).setEnabled(mEnableMenuItem );
在 OnActivityResult 中:
mEnableMenuItem = true;
where are you calling this?(Sorry, didn't read carefully) I think you need to call it after the menu is inflated (usually in OnCreateOptionsMenu). To do this, you can set a variable to true when the other Activity returns, then do((MenuItem)findViewById(R.id.menu_how)).setEnabled(mMyBooleanField)
in OnCreateOptionsMenu after the call to inflater.inflate.Edit: To accomplish this in code, it might look something like this:
At the top of the class (along with all the other class members):
Boolean mEnableMenuItem = false;
In OnCreateOptionsMenu:
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_main, menu);
((MenuItem)findViewById(R.id.menu_how)).setEnabled(mEnableMenuItem );
In OnActivityResult:
mEnableMenuItem = true;
在您的活动中保留对菜单的引用:
然后:
现在,要访问活动中任何位置的菜单项,请使用与此类似的代码:
或
Keep a reference to Menu in your activity:
Then:
Now, to access menu items anywhere in your activity use similar code to this:
or
尝试在 onCreateOptionsMenu 中使用
menu.findItem(R.id.menu_how)
并保存参考以供以后使用。这在
enabled
下应该可以正常工作,但是,我发现在 XML 中将菜单项设置为不可见意味着您无法以编程方式显示/隐藏它。Try using
menu.findItem(R.id.menu_how)
in onCreateOptionsMenu and save a reference for later use.This should work fine with
enabled
, however, I've found that setting a menu item to invisible in the XML means you can't show/hide it programmatically.我在 android dev site 上发现了一些可能有用的东西(寻找“在运行时更改菜单项”部分)
它说 onCreateOptionsMenu() 方法仅在创建活动的菜单时触发,并且在该活动启动时发生。因此,如果您想在创建菜单/活动后更改菜单项,则需要重写
onPrepareOptionsMenu()
方法。搜索链接以获取完整详细信息。编辑:
刚刚制作完成并且工作正常。我对每个
menuItem
使用一个boolean var
来表示是否应启用此项目。这是我的代码:I found something at the android dev site that might be helpful (look for the section "Changing menu items at runtime")
It said that the
onCreateOptionsMenu()
method fired only when the the menu for the activity is created, and it happens when this activity starts. So if you want to change the menu items after the menu/activity was created, you need to override theonPrepareOptionsMenu()
method instead. search the link for full details.EDIT:
Just made it and it's working fine. I'm using one
boolean var
permenuItem
which represents if this item should be enabled or not. This is my code: