Android 从一个按钮打开菜单 [英] Android Open Menu from a button
问题描述
有没有办法让按钮调用子菜单?
Is there a way for a button to call a submenu?
我目前有一个菜单,如果他们选择其中一个选项,它会打开一个子菜单,但是我希望能够从 Button 小部件而不是 Menu 对象启动子菜单.
I currently have a menu where if they select one of the options it opens a submenu, however I want to be able to start the submenu from the Button widget instead of the Menu object.
谢谢!
推荐答案
我不一定推荐这个,但我能够以编程方式调用子菜单.首先,在 onCreateOptionsMenu 的覆盖中,我将菜单参数存储到活动中的私有 Menu 变量中.然后,我可以简单地从 onClick() 执行以下操作:
I am not necessarily recommending this, but I was able to call the submenu programatically. First, in the override of onCreateOptionsMenu I stored the menu parameter to a private Menu variable in the activity. Then, I was able to simply do the following from the onClick():
MyActivity.this.openOptionsMenu(); // activity's onCreateOptionsMenu gets called
optionsMenu.performIdentifierAction(R.id.miSubMenu1, 0);
在一个特定的例子中,这为我节省了很多时间,因为子菜单是在请求直接进入子菜单之前几个月建立的.
In a particular instance this saved me a lot of time since the submenu was built months before the request to have an additional button go directly to the submenu.
这篇关于Android 从一个按钮打开菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!