Android: How to add listener to hardware menu button?

前端 未结 4 1377
时光说笑
时光说笑 2021-01-01 12:09

I\'m currently trying to add a click listener to the menu hardware button. Currently I\'m just putting my onclick logic into the onCreatePanelMenu-method and return false. B

4条回答
  •  旧时难觅i
    2021-01-01 12:44

    Catch the key event inside onKeyDown() and add your action there.

    Sample:

    @Override
    public boolean onKeyDown(int keycode, KeyEvent e) {
        switch(keycode) {
            case KeyEvent.KEYCODE_MENU:
                doSomething();
                return true;
        }
    
        return super.onKeyDown(keycode, e);
    }
    

    Just replace doSomething() with your functionality/methods.

提交回复
热议问题