简体   繁体   English

来自onOptionsItemSelected的Android访问菜单

[英]Android access menu from onOptionsItemSelected

I have the following in my activity (sorry new to Java/Android): 我的活动中有以下内容(对Java / Android来说很抱歉):

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.options, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case R.id.selectItem:
            // menu.add(...) --> how to get the menu instance?
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

I am wondering, how can I access a menu object in onOptionsItemSelected? 我想知道如何在onOptionsItemSelected中访问菜单对象? For example, how would I go about adding a new view to the options menu based on the selection of an existing menu item? 例如,如何根据现有菜单项的选择向选项菜单添加新视图? Is the answer related to "onPrepareOptionsMenu"? 答案与“ onPrepareOptionsMenu”有关吗?

您应该对此类菜单使用SubMenu ... 记住您不能将子菜单添加到另一个子菜单中...所以只有Menu-> Submenu才是可能的,您不能做类似Menu-> Submenu-> Submenu的操作(而Submenu是Dialog选择)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM