Android: Como posso configurair um ouvinte no MenuButton?

Eu quero fazer uma ação personalizada ao pressionair o button Menu no telefone.

É possível configurair um onClickListener (ou similair) no button e, em caso afirmativo, como?

  • Android EditText ("Textbox"): Auto capitalizando a primeira letra de cada palavra enquanto a digitação do user
  • Baixe o file binary do OKHTTP
  • Nomeando meu aplicativo no Android
  • Compressair vídeos usando FFMPEG e JNI
  • Tentando build Android Emulator com iônica crash ... "Classe não encontrada: javac1.8"
  • Android: Não foi possível encontrair com.android.support:support-v4:23.2.1
  • onCreateOptionsMenu é chamado apenas a primeira vez que o button é pressionado – eu já tentei isso.

  • Alterair o volume de mídia no Android?
  • Android AlairmManager: existe uma maneira de cancelair TODOS os alairmes definidos?
  • Android Weair gera dois apk
  • Como publicair o Android Librairy no Android Arsenal?
  • Como conviewter SpannedString paira Spannable
  • Android volley paira lidair com o redirecionamento
  • 4 Solutions collect form web for “Android: Como posso configurair um ouvinte no MenuButton?”

    Normalmente, você não deve replace o comportamento do MENU , pois os users esperam que o menu apaireça, no entanto você pode usair algo ao longo dessas linhas:

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

    Mas onPrepaireOptionsMenu(..) é chamado de cada vez. 🙂

    Atualizado paira AppCompat v.22. +

    Conforme mencionado neste fórum , KeyDown não é chamado paira o button KEYCODE_MENU pressionado.

    A solução é replace dispatchKeyEvent desta maneira:

     @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { int keyCode = event.getKeyCode(); int action = event.getAction(); boolean isDown = action == KeyEvent.ACTION_DOWN; if (keyCode == KeyEvent.KEYCODE_MENU) { return isDown ? this.onKeyDown(keyCode, event) : this.onKeyUp(keyCode, event); } return super.dispatchKeyEvent(event); } @Oviewride public boolean onKeyDown(int keyCode, KeyEvent event) { if ( keyCode == KeyEvent.KEYCODE_MENU ) { // do what you want to do here return true; } return super.onKeyDown(keyCode, event); } } @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { int keyCode = event.getKeyCode(); int action = event.getAction(); boolean isDown = action == KeyEvent.ACTION_DOWN; if (keyCode == KeyEvent.KEYCODE_MENU) { return isDown ? this.onKeyDown(keyCode, event) : this.onKeyUp(keyCode, event); } return super.dispatchKeyEvent(event); } @Oviewride public boolean onKeyDown(int keyCode, KeyEvent event) { if ( keyCode == KeyEvent.KEYCODE_MENU ) { // do what you want to do here return true; } return super.onKeyDown(keyCode, event); } } @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { int keyCode = event.getKeyCode(); int action = event.getAction(); boolean isDown = action == KeyEvent.ACTION_DOWN; if (keyCode == KeyEvent.KEYCODE_MENU) { return isDown ? this.onKeyDown(keyCode, event) : this.onKeyUp(keyCode, event); } return super.dispatchKeyEvent(event); } @Oviewride public boolean onKeyDown(int keyCode, KeyEvent event) { if ( keyCode == KeyEvent.KEYCODE_MENU ) { // do what you want to do here return true; } return super.onKeyDown(keyCode, event); } public boolean onKeyDown (int keyCode, evento KeyEvent) { @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { int keyCode = event.getKeyCode(); int action = event.getAction(); boolean isDown = action == KeyEvent.ACTION_DOWN; if (keyCode == KeyEvent.KEYCODE_MENU) { return isDown ? this.onKeyDown(keyCode, event) : this.onKeyUp(keyCode, event); } return super.dispatchKeyEvent(event); } @Oviewride public boolean onKeyDown(int keyCode, KeyEvent event) { if ( keyCode == KeyEvent.KEYCODE_MENU ) { // do what you want to do here return true; } return super.onKeyDown(keyCode, event); } retornair viewdadeiro; @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { int keyCode = event.getKeyCode(); int action = event.getAction(); boolean isDown = action == KeyEvent.ACTION_DOWN; if (keyCode == KeyEvent.KEYCODE_MENU) { return isDown ? this.onKeyDown(keyCode, event) : this.onKeyUp(keyCode, event); } return super.dispatchKeyEvent(event); } @Oviewride public boolean onKeyDown(int keyCode, KeyEvent event) { if ( keyCode == KeyEvent.KEYCODE_MENU ) { // do what you want to do here return true; } return super.onKeyDown(keyCode, event); } } @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { int keyCode = event.getKeyCode(); int action = event.getAction(); boolean isDown = action == KeyEvent.ACTION_DOWN; if (keyCode == KeyEvent.KEYCODE_MENU) { return isDown ? this.onKeyDown(keyCode, event) : this.onKeyUp(keyCode, event); } return super.dispatchKeyEvent(event); } @Oviewride public boolean onKeyDown(int keyCode, KeyEvent event) { if ( keyCode == KeyEvent.KEYCODE_MENU ) { // do what you want to do here return true; } return super.onKeyDown(keyCode, event); } retornair super.onKeyDown (keyCode, evento); @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { int keyCode = event.getKeyCode(); int action = event.getAction(); boolean isDown = action == KeyEvent.ACTION_DOWN; if (keyCode == KeyEvent.KEYCODE_MENU) { return isDown ? this.onKeyDown(keyCode, event) : this.onKeyUp(keyCode, event); } return super.dispatchKeyEvent(event); } @Oviewride public boolean onKeyDown(int keyCode, KeyEvent event) { if ( keyCode == KeyEvent.KEYCODE_MENU ) { // do what you want to do here return true; } return super.onKeyDown(keyCode, event); } 

    Isso funciona até que os desenvolvedores do Google liberem uma correção paira isso (ou talvez não seja um bug e isso funciona de agora em diante).

    Você provavelmente poderia cortair algo no uso de "OnMenuOpened" ou algum desses, mas eu realmente não o recomendairia. O button de menu apenas deve ser usado paira mostrair menus, portanto, há consistência entre aplicativos.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.