禁用Android的操作栏按钮 [英] Disable Action Bar button in Android

查看:121
本文介绍了禁用Android的操作栏按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能禁止在Android中的操作栏按钮?我环顾四周,我无法找到任何code代码片段,我想应该有一些简单的方法。

Is it possible to disable a button in Action Bar in Android? I was looking around and I could not find any code snippet for that, I was thinking that there should be some easy way.

推荐答案

1)当你执行用户操作时要禁用的操作栏,设置一些标志,说disableButtonFlag。

1.) Once you perform the user action when you want to disable the action bar, set some flag, say disableButtonFlag.

2)调用invalidateOptionsMenu()。这将触发onCreateOptionsMenu被调用,以重新生成菜单。

2.) Call invalidateOptionsMenu(). This will trigger onCreateOptionsMenu to be invoked to regenerate your menu.

3)。最后修改onCreateOptionsMenu禁用要取决于disableButtonFlag的状态的按钮。

3.) Finally modify your onCreateOptionsMenu to disable the button you want depending on the state of disableButtonFlag.

if (disableButtonFlag) {
    menu.findItem(R.id.your_item).setEnabled(false);
} else {
    menu.findItem(R.id.your_item).setEnabled(true);
}

或者更简单地说:

Or more simply:

menu.findItem(R.id.your_item).setEnabled(!disableButtonFlag);

这篇关于禁用Android的操作栏按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆