且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-09-11 20:48:16

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

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().