在同一个 activity 中单击按钮时创建一个新的菜单选项菜单

create a new menu option menu on button click in the same activity

我是 android 的新手,我最近研究了选项菜单的文档,我不了解其中的内容 onPrerpareOptionMenu() .在我的应用程序中,当单击一个按钮时,我想更改相同 activity.

的选项菜单

谢谢...

  public boolean onPrepareOptionsMenu(Menu menu){
    return false;
    menu.clear(); //Clear view of previous menu
    MenuInflater inflater = getMenuInflater();
    if( )//condition 
        inflater.inflate(R.menu.view_record, menu);
    else
        inflater.inflate(R.menu.add_record, menu);
    return super.onPrepareOptionsMenu(menu);
}

我在我的几个相机应用程序中这样做,以根据通过滑动控件启用的相机功能(尽管按钮也可以)更改菜单,即用于捕获图像或视频。

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    MenuItem someThing1 = menu.findItem(R.id.someThing1);
    MenuItem someThing2 = menu.findItem(R.id.someThing2);
    MenuItem someThing3 = menu.findItem(R.id.someThing3);
    MenuItem someThing4 = menu.findItem(R.id.someThing4);
    MenuItem someThing5 = menu.findItem(R.id.someThing5);
    MenuItem help = menu.findItem(R.id.help);
    MenuItem about = menu.findItem(R.id.about);
    if (some_condition) { //could be button state or..?
        someThing1.setTitle(R.string.someThing1a);
        someThing2.setTitle(R.string.someThing2a);
        someThing3.setTitle(R.string.someThing3a);
        someThing4.setVisible(false);
        someThing5.setVisible(false);
        help.setVisible(false);
        about.setVisible(false);
    } else {
        someThing1.setTitle(R.string.someThing1b);
        someThing2.setTitle(R.string.someThing2b);
        someThing3.setTitle(R.string.someThing3b);
        someThing4.setVisible(true);
        someThing5.setVisible(true);
        help.setVisible(true);
        about.setVisible(true);
    }
     return true; // this is important to call so that new menu is shown
}