禁用Android的操作栏按钮按钮、操作、Android

由网友(吟雪情枫)分享简介:是否有可能禁止在Android中的操作栏按钮?我环顾四周,我无法找到任何code代码片段,我想应该有一些简单的方法。Is it possible to disable a button in Action Bar in Android? I was looking around and I could not fin...

是否有可能禁止在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);
阅读全文

相关推荐

最新文章