How to open the options menu programmatically?

前端 未结 11 1631
时光说笑
时光说笑 2020-11-28 06:47

I would like to open the optionsMenu programmatically without a click on the menu key from the user. How would I do that?

11条回答
  •  萌比男神i
    2020-11-28 07:09

    After a long research and many tries, the only way seems to be simulating a KeyEvent. This makes the options menu appear:

    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);
    

提交回复
热议问题