Android 如何以编程方式打开选项菜单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3133318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open the options menu programmatically?
提问by BlaBRA
I would like to open the optionsMenu programmatically without a click on the menu key from the user. How would I do that?
我想以编程方式打开 optionsMenu ,而无需单击用户的菜单键。我该怎么做?
回答by Robby Pond
Or just call Activity.openOptionsMenu()?
或者只是调用Activity.openOptionsMenu()?
回答by Kova?
Apparently, doing it in onCreate breaks app, since Activity's not yet attached to a window. If you do it like so:
显然,在 onCreate 中执行此操作会中断应用程序,因为 Activity 尚未附加到窗口。如果你这样做:
@Override
public void onAttachedToWindow() {
openOptionsMenu();
};
...it works.
...有用。
回答by marmor
For developers using the new Toolbar
class of the Support Library
, this is how it's done:
对于使用 的新Toolbar
类的开发人员Support Library
,这是如何完成的:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.showOverflowMenu();
回答by Royston Pinto
Put this line of code in your onResume(), this should help!
将这行代码放在您的 onResume() 中,这应该会有所帮助!
new Handler().postDelayed(new Runnable() {
public void run() {
openOptionsMenu();
}
}, 1000);
回答by Oded Breiner
from an OnClickListener inside an activity called MainActivity:
来自名为 MainActivity 的活动中的 OnClickListener:
MainActivity.this.openOptionsMenu();
回答by Abhijit
if using AppCompatActivity
如果使用 AppCompatActivity
getSupportActionBar().openOptionsMenu();
回答by Cristian
Two ways to do it:
有两种方法可以做到:
Activity.getWindow().openPanel(Window.FEATURE_OPTIONS_PANEL, event);
The event
argument is a KeyEvent
describing the key used to open the menu, which can modify how the menu is displayed based on the type of keyboard it came from.
该event
参数是一个KeyEvent
描述用来打开菜单键,可以修改菜单,是如何根据它来自键盘类型显示。
Or... by simulating that the user has pressed the button:
或者...通过模拟用户按下了按钮:
IWindowManager wManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
KeyEvent kd = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SOFT_LEFT);
KeyEvent ku = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_SOFT_LEFT);
wManager.injectKeyEvent(kd.isDown(), kd.getKeyCode(), kd.getRepeatCount(), kd.getDownTime(), kd.getEventTime(), true);
回答by Lotfi
After a long research and many tries, the only way seems to be simulating a KeyEvent
. This makes the options menu appear:
经过长时间的研究和多次尝试,唯一的方法似乎是模拟KeyEvent
. 这使选项菜单出现:
BaseInputConnection mInputConnection = new BaseInputConnection( findViewById(R.id.main_content), true);
KeyEvent kd = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU);
KeyEvent ku = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU);
mInputConnection.sendKeyEvent(kd);
mInputConnection.sendKeyEvent(ku);
回答by Nanda Gopal
For me, declaring toolbar.showOverflowMenu()
in onClick is not worked. openOptionsMenu()
also not worked for me. Instead of that the following way is worked for me,
对我来说,toolbar.showOverflowMenu()
在 onClick 中声明是行不通的。openOptionsMenu()
也不适合我。而不是以下方式对我有用,
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
toolbar.showOverflowMenu();
}
}, 500);
回答by Mukesh KrishMeg
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(0xFFFFFFFF);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
toolbar.showOverflowMenu();
}
}, 100);