android: changing option menu items programmatically

前端 未结 12 1953
终归单人心
终归单人心 2020-12-02 09:33

Is it possible to change the option menu items programmatically? Can anyone provide me with an example please?

Also, I want to disable certain items, so that they do

12条回答
  •  没有蜡笔的小新
    2020-12-02 10:06

    If I have to change the contents of my options menu I perform it during the onMenuOpened(). This allows me to check the running state at the very moment that the user is accessing the menu.

    public boolean onMenuOpened(int featureid, Menu menu)
        {
            menu.clear();
            if (!editable)
            {
                MenuItem itemAdd = menu.add(0, REASSIGN, Menu.NONE, context.getString(R.string.reassign));
                MenuItem itemMod = menu.add(1, EDIT, Menu.NONE, context.getString(R.string.modify));
                MenuItem itemDel = menu.add(2, DELETE, Menu.NONE, context.getString(R.string.delete));
                itemAdd.setShortcut('0', 'a');
                itemMod.setShortcut('1', 'm');
                itemDel.setShortcut('2', 'd');
            }
            else
            {
                MenuItem itemSave = menu.add(3, SAVE, Menu.NONE, context.getString(R.string.savechanges));
                itemSave.setShortcut('0', 'S');
            }
    
    
            return true;
        }
    

提交回复
热议问题