在动作条的动作/菜单项动态控制 [英] Dynamic control of action/menu items in ActionBar
本文介绍了在动作条的动作/菜单项动态控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以动态关闭,隐藏,添加/删除ActionBar的菜单项?例如,一个动作被禁用,直到用户填写一个活动一个有效的电话号码。
Is there a way to dynamically disable , hide, add/remove menu items in ActionBar ? For example, an action is disabled until user fills a valid phone number in an activity.
我没有找到在动作条API任何有用的方法,只有这样,好像是用动作条中的自定义视图。
I didn't find any useful methods in ActionBar API, the only way seems to be using a custom View in ActionBar.
推荐答案
要告诉动作条,刷新其菜单项:的 invalidateOptionsMenu()
To tell ActionBar to refresh its menu items: invalidateOptionsMenu()
然后启用/禁用菜单项:
then to enable/disable Menu Items:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item= menu.findItem(R.id.men_1);
//depending on your conditions, either enable/disable
item.setEnabled(false);
super.onPrepareOptionsMenu(menu);
return true;
}
和隐藏操作栏有:
getActionBar().hide();
这篇关于在动作条的动作/菜单项动态控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文