Android: How can I set a listener to the MenuButton?

后端 未结 4 1818
野性不改
野性不改 2020-12-16 15:29

I want to do a custom action when pressing on the Menu button on the phone.

Is it possible to set an onClickListener (or similar) on the button and if s

4条回答
  •  暖寄归人
    2020-12-16 15:57

    Usually you shouldn't override MENU behavior as users expect menu to appear, however you can use something along these lines:

    /* (non-Javadoc)
     * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ( keyCode == KeyEvent.KEYCODE_MENU ) {
            Log.d(TAG, "MENU pressed");
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    

提交回复
热议问题