Android的:如何启用按钮/禁用选项菜单项,点击?菜单项、按钮、选项、Android

2023-09-12 00:47:00 作者:追逐我的明天。

我可以很容易地做到这一点,当我使用 onCreateOptionsMenu onOptionsItemSelected 的方法。

I can easily do it when I am using onCreateOptionsMenu or onOptionsItemSelected methods.

不过,我有一个按钮某处屏幕,并点击该按钮,就应该启用/禁用上下文菜单项。

But I have a button somewhere in screen, and on clicking that button, it should enable/disable context menu items.

谢谢...

推荐答案

总之,文档涵盖了所有的事情。

Anyway, the documentation covers all the things.

在该活动创建的,则    onCreateOptionsMenu()方法被调用   只有一次,如上所述。该   系统保持并重新使用菜单您   定义此方法,直到您   活性被破坏。如果你想   任何时间之后更改选项菜单   它是第一次创建,必须覆盖   在在prepareOptionsMenu()方法。   这通过你的菜单对象,因为它   目前存在。这是如果有用的话   你想删除,添加,禁用,或   使根据菜单项   应用程序的当前状态。

Once the activity is created, the onCreateOptionsMenu() method is called only once, as described above. The system keeps and re-uses the Menu you define in this method until your activity is destroyed. If you want to change the Options Menu any time after it's first created, you must override the onPrepareOptionsMenu() method. This passes you the Menu object as it currently exists. This is useful if you'd like to remove, add, disable, or enable menu items depending on the current state of your application.

例如。

@Override
public boolean onPrepareOptionsMenu (Menu menu) {
    if (isFinalized)
        menu.getItem(1).setEnabled(false);
    return true;
}

在Android 3.0及更高版本,选项菜单被认为始终是打开的,当菜单项psented操作栏中$ P $。当事件发生时,你要执行菜单更新,您必须调用 invalidateOptionsMenu()要求,该系统调用在prepareOptionsMenu( )

On Android 3.0 and higher, the options menu is considered to always be open when menu items are presented in the action bar. When an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu() to request that the system call onPrepareOptionsMenu().