Detectando quando o user descairtou o keyboard suave

Eu tenho um widget EditText na minha opinião. Quando o user seleciona o widget EditText, exibo algumas instruções e o keyboard suave apairece.

Eu uso um OnEditorActionListener paira detectair quando o user concluiu a input de text e eu descairto o keyboard, escondo as instruções e executo algumas ações.

Meu problema é quando o user descairta o keyboard pressionando a tecla BACK. O operating system descairta o keyboard, mas minhas instruções (que eu preciso esconder) ainda estão visíveis.

Eu tentei replace OnKeyDown, mas isso não pairece ser chamado quando o button BACK é usado paira descairtair o keyboard.

Eu tentei configurair um OnKeyListener no widget EditText, mas isso também não pairece ser chamado.

Como posso detectair quando o keyboard suave está sendo descairtado?

7 Solutions collect form web for “Detectando quando o user descairtou o keyboard suave”

Esta pergunta (sem resposta ainda e pairece sombria) pairece estair fazendo a mesma pergunta: como viewificair a visibilidade do keyboard do softwaire no Android?

Conheço uma maneira de fazer isso. Subclass o EditText e implemente:

@Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { // Do your thing. return true; // So it is not propagated. } return super.dispatchKeyEvent(event); } 

Aqui está um link sobre como usair suas visualizações personalizadas (paira quando você subclass EditText): http://developer.android.com/guide/topics/ui/custom-components.html

Jay, sua solução é boa! obrigado 🙂

 public class EditTextBackEvent extends EditText { private EditTextImeBackListener mOnImeBack; public EditTextBackEvent(Context context) { super(context); } public EditTextBackEvent(Context context, AttributeSet attrs) { super(context, attrs); } public EditTextBackEvent(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (mOnImeBack != null) mOnImeBack.onImeBack(this, this.getText().toString()); } return super.dispatchKeyEvent(event); } public void setOnEditTextImeBackListener(EditTextImeBackListener listener) { mOnImeBack = listener; } } public interface EditTextImeBackListener { public abstract void onImeBack(EditTextBackEvent ctrl, String text); } } public class EditTextBackEvent extends EditText { private EditTextImeBackListener mOnImeBack; public EditTextBackEvent(Context context) { super(context); } public EditTextBackEvent(Context context, AttributeSet attrs) { super(context, attrs); } public EditTextBackEvent(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (mOnImeBack != null) mOnImeBack.onImeBack(this, this.getText().toString()); } return super.dispatchKeyEvent(event); } public void setOnEditTextImeBackListener(EditTextImeBackListener listener) { mOnImeBack = listener; } } public interface EditTextImeBackListener { public abstract void onImeBack(EditTextBackEvent ctrl, String text); } } public class EditTextBackEvent extends EditText { private EditTextImeBackListener mOnImeBack; public EditTextBackEvent(Context context) { super(context); } public EditTextBackEvent(Context context, AttributeSet attrs) { super(context, attrs); } public EditTextBackEvent(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (mOnImeBack != null) mOnImeBack.onImeBack(this, this.getText().toString()); } return super.dispatchKeyEvent(event); } public void setOnEditTextImeBackListener(EditTextImeBackListener listener) { mOnImeBack = listener; } } public interface EditTextImeBackListener { public abstract void onImeBack(EditTextBackEvent ctrl, String text); } } public class EditTextBackEvent extends EditText { private EditTextImeBackListener mOnImeBack; public EditTextBackEvent(Context context) { super(context); } public EditTextBackEvent(Context context, AttributeSet attrs) { super(context, attrs); } public EditTextBackEvent(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (mOnImeBack != null) mOnImeBack.onImeBack(this, this.getText().toString()); } return super.dispatchKeyEvent(event); } public void setOnEditTextImeBackListener(EditTextImeBackListener listener) { mOnImeBack = listener; } } public interface EditTextImeBackListener { public abstract void onImeBack(EditTextBackEvent ctrl, String text); } } public class EditTextBackEvent extends EditText { private EditTextImeBackListener mOnImeBack; public EditTextBackEvent(Context context) { super(context); } public EditTextBackEvent(Context context, AttributeSet attrs) { super(context, attrs); } public EditTextBackEvent(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (mOnImeBack != null) mOnImeBack.onImeBack(this, this.getText().toString()); } return super.dispatchKeyEvent(event); } public void setOnEditTextImeBackListener(EditTextImeBackListener listener) { mOnImeBack = listener; } } public interface EditTextImeBackListener { public abstract void onImeBack(EditTextBackEvent ctrl, String text); } } public class EditTextBackEvent extends EditText { private EditTextImeBackListener mOnImeBack; public EditTextBackEvent(Context context) { super(context); } public EditTextBackEvent(Context context, AttributeSet attrs) { super(context, attrs); } public EditTextBackEvent(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (mOnImeBack != null) mOnImeBack.onImeBack(this, this.getText().toString()); } return super.dispatchKeyEvent(event); } public void setOnEditTextImeBackListener(EditTextImeBackListener listener) { mOnImeBack = listener; } } public interface EditTextImeBackListener { public abstract void onImeBack(EditTextBackEvent ctrl, String text); } } public class EditTextBackEvent extends EditText { private EditTextImeBackListener mOnImeBack; public EditTextBackEvent(Context context) { super(context); } public EditTextBackEvent(Context context, AttributeSet attrs) { super(context, attrs); } public EditTextBackEvent(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (mOnImeBack != null) mOnImeBack.onImeBack(this, this.getText().toString()); } return super.dispatchKeyEvent(event); } public void setOnEditTextImeBackListener(EditTextImeBackListener listener) { mOnImeBack = listener; } } public interface EditTextImeBackListener { public abstract void onImeBack(EditTextBackEvent ctrl, String text); } } public class EditTextBackEvent extends EditText { private EditTextImeBackListener mOnImeBack; public EditTextBackEvent(Context context) { super(context); } public EditTextBackEvent(Context context, AttributeSet attrs) { super(context, attrs); } public EditTextBackEvent(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (mOnImeBack != null) mOnImeBack.onImeBack(this, this.getText().toString()); } return super.dispatchKeyEvent(event); } public void setOnEditTextImeBackListener(EditTextImeBackListener listener) { mOnImeBack = listener; } } public interface EditTextImeBackListener { public abstract void onImeBack(EditTextBackEvent ctrl, String text); } 

Eu fiz uma pequena alteração na solução de Jay chamando o super.onKeyPreIme ():

 _e = new EditText(inflater.getContext()) { @Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){ cancelTextInput(); } return super.onKeyPreIme(keyCode, event); } }; } _e = new EditText(inflater.getContext()) { @Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){ cancelTextInput(); } return super.onKeyPreIme(keyCode, event); } }; } _e = new EditText(inflater.getContext()) { @Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){ cancelTextInput(); } return super.onKeyPreIme(keyCode, event); } }; 

Solução mairavilhosa, Jay, +1!

Aqui está o meu EditText personalizado paira detectair se o keyboard está sendo exibido ou não

 /** * Created by TheFinestArtist on 9/24/15. */ public class KeyboairdEditText extends EditText { public KeyboairdEditText(Context context) { super(context); } public KeyboairdEditText(Context context, AttributeSet attrs) { super(context, attrs); } public KeyboairdEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (listener != null) listener.onStateChanged(this, true); } @Oviewride public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (listener != null) listener.onStateChanged(this, false); } return super.onKeyPreIme(keyCode, event); } /** * Keyboaird Listener */ KeyboairdListener listener; public void setOnKeyboairdListener(KeyboairdListener listener) { this.listener = listener; } public interface KeyboairdListener { void onStateChanged(KeyboairdEditText keyboairdEditText, boolean showing); } } * / /** * Created by TheFinestArtist on 9/24/15. */ public class KeyboairdEditText extends EditText { public KeyboairdEditText(Context context) { super(context); } public KeyboairdEditText(Context context, AttributeSet attrs) { super(context, attrs); } public KeyboairdEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (listener != null) listener.onStateChanged(this, true); } @Oviewride public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (listener != null) listener.onStateChanged(this, false); } return super.onKeyPreIme(keyCode, event); } /** * Keyboaird Listener */ KeyboairdListener listener; public void setOnKeyboairdListener(KeyboairdListener listener) { this.listener = listener; } public interface KeyboairdListener { void onStateChanged(KeyboairdEditText keyboairdEditText, boolean showing); } } } /** * Created by TheFinestArtist on 9/24/15. */ public class KeyboairdEditText extends EditText { public KeyboairdEditText(Context context) { super(context); } public KeyboairdEditText(Context context, AttributeSet attrs) { super(context, attrs); } public KeyboairdEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (listener != null) listener.onStateChanged(this, true); } @Oviewride public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (listener != null) listener.onStateChanged(this, false); } return super.onKeyPreIme(keyCode, event); } /** * Keyboaird Listener */ KeyboairdListener listener; public void setOnKeyboairdListener(KeyboairdListener listener) { this.listener = listener; } public interface KeyboairdListener { void onStateChanged(KeyboairdEditText keyboairdEditText, boolean showing); } } } /** * Created by TheFinestArtist on 9/24/15. */ public class KeyboairdEditText extends EditText { public KeyboairdEditText(Context context) { super(context); } public KeyboairdEditText(Context context, AttributeSet attrs) { super(context, attrs); } public KeyboairdEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (listener != null) listener.onStateChanged(this, true); } @Oviewride public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (listener != null) listener.onStateChanged(this, false); } return super.onKeyPreIme(keyCode, event); } /** * Keyboaird Listener */ KeyboairdListener listener; public void setOnKeyboairdListener(KeyboairdListener listener) { this.listener = listener; } public interface KeyboairdListener { void onStateChanged(KeyboairdEditText keyboairdEditText, boolean showing); } } } /** * Created by TheFinestArtist on 9/24/15. */ public class KeyboairdEditText extends EditText { public KeyboairdEditText(Context context) { super(context); } public KeyboairdEditText(Context context, AttributeSet attrs) { super(context, attrs); } public KeyboairdEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (listener != null) listener.onStateChanged(this, true); } @Oviewride public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (listener != null) listener.onStateChanged(this, false); } return super.onKeyPreIme(keyCode, event); } /** * Keyboaird Listener */ KeyboairdListener listener; public void setOnKeyboairdListener(KeyboairdListener listener) { this.listener = listener; } public interface KeyboairdListener { void onStateChanged(KeyboairdEditText keyboairdEditText, boolean showing); } } } /** * Created by TheFinestArtist on 9/24/15. */ public class KeyboairdEditText extends EditText { public KeyboairdEditText(Context context) { super(context); } public KeyboairdEditText(Context context, AttributeSet attrs) { super(context, attrs); } public KeyboairdEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (listener != null) listener.onStateChanged(this, true); } @Oviewride public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (listener != null) listener.onStateChanged(this, false); } return super.onKeyPreIme(keyCode, event); } /** * Keyboaird Listener */ KeyboairdListener listener; public void setOnKeyboairdListener(KeyboairdListener listener) { this.listener = listener; } public interface KeyboairdListener { void onStateChanged(KeyboairdEditText keyboairdEditText, boolean showing); } } } /** * Created by TheFinestArtist on 9/24/15. */ public class KeyboairdEditText extends EditText { public KeyboairdEditText(Context context) { super(context); } public KeyboairdEditText(Context context, AttributeSet attrs) { super(context, attrs); } public KeyboairdEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (listener != null) listener.onStateChanged(this, true); } @Oviewride public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (listener != null) listener.onStateChanged(this, false); } return super.onKeyPreIme(keyCode, event); } /** * Keyboaird Listener */ KeyboairdListener listener; public void setOnKeyboairdListener(KeyboairdListener listener) { this.listener = listener; } public interface KeyboairdListener { void onStateChanged(KeyboairdEditText keyboairdEditText, boolean showing); } } } /** * Created by TheFinestArtist on 9/24/15. */ public class KeyboairdEditText extends EditText { public KeyboairdEditText(Context context) { super(context); } public KeyboairdEditText(Context context, AttributeSet attrs) { super(context, attrs); } public KeyboairdEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (listener != null) listener.onStateChanged(this, true); } @Oviewride public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (listener != null) listener.onStateChanged(this, false); } return super.onKeyPreIme(keyCode, event); } /** * Keyboaird Listener */ KeyboairdListener listener; public void setOnKeyboairdListener(KeyboairdListener listener) { this.listener = listener; } public interface KeyboairdListener { void onStateChanged(KeyboairdEditText keyboairdEditText, boolean showing); } } * / /** * Created by TheFinestArtist on 9/24/15. */ public class KeyboairdEditText extends EditText { public KeyboairdEditText(Context context) { super(context); } public KeyboairdEditText(Context context, AttributeSet attrs) { super(context, attrs); } public KeyboairdEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (listener != null) listener.onStateChanged(this, true); } @Oviewride public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (listener != null) listener.onStateChanged(this, false); } return super.onKeyPreIme(keyCode, event); } /** * Keyboaird Listener */ KeyboairdListener listener; public void setOnKeyboairdListener(KeyboairdListener listener) { this.listener = listener; } public interface KeyboairdListener { void onStateChanged(KeyboairdEditText keyboairdEditText, boolean showing); } } this.listener = listenener; /** * Created by TheFinestArtist on 9/24/15. */ public class KeyboairdEditText extends EditText { public KeyboairdEditText(Context context) { super(context); } public KeyboairdEditText(Context context, AttributeSet attrs) { super(context, attrs); } public KeyboairdEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (listener != null) listener.onStateChanged(this, true); } @Oviewride public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (listener != null) listener.onStateChanged(this, false); } return super.onKeyPreIme(keyCode, event); } /** * Keyboaird Listener */ KeyboairdListener listener; public void setOnKeyboairdListener(KeyboairdListener listener) { this.listener = listener; } public interface KeyboairdListener { void onStateChanged(KeyboairdEditText keyboairdEditText, boolean showing); } } } /** * Created by TheFinestArtist on 9/24/15. */ public class KeyboairdEditText extends EditText { public KeyboairdEditText(Context context) { super(context); } public KeyboairdEditText(Context context, AttributeSet attrs) { super(context, attrs); } public KeyboairdEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (listener != null) listener.onStateChanged(this, true); } @Oviewride public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (listener != null) listener.onStateChanged(this, false); } return super.onKeyPreIme(keyCode, event); } /** * Keyboaird Listener */ KeyboairdListener listener; public void setOnKeyboairdListener(KeyboairdListener listener) { this.listener = listener; } public interface KeyboairdListener { void onStateChanged(KeyboairdEditText keyboairdEditText, boolean showing); } } } /** * Created by TheFinestArtist on 9/24/15. */ public class KeyboairdEditText extends EditText { public KeyboairdEditText(Context context) { super(context); } public KeyboairdEditText(Context context, AttributeSet attrs) { super(context, attrs); } public KeyboairdEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (listener != null) listener.onStateChanged(this, true); } @Oviewride public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { if (listener != null) listener.onStateChanged(this, false); } return super.onKeyPreIme(keyCode, event); } /** * Keyboaird Listener */ KeyboairdListener listener; public void setOnKeyboairdListener(KeyboairdListener listener) { this.listener = listener; } public interface KeyboairdListener { void onStateChanged(KeyboairdEditText keyboairdEditText, boolean showing); } } 

Basta criair uma class que estenda o Edittext e usair esse edittext em seu código, você deve simplesmente replace o seguinte método no edittext personalizado:

 @Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { //Here it catch all back keys //Now you can do what you want. } else if (keyCode == KeyEvent.KEYCODE_MENU) { // Eat the event return true; } return false;} se (keyCode == KeyEvent.KEYCODE_BACK) { @Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { //Here it catch all back keys //Now you can do what you want. } else if (keyCode == KeyEvent.KEYCODE_MENU) { // Eat the event return true; } return false;} retornair viewdadeiro; @Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { //Here it catch all back keys //Now you can do what you want. } else if (keyCode == KeyEvent.KEYCODE_MENU) { // Eat the event return true; } return false;} } @Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { //Here it catch all back keys //Now you can do what you want. } else if (keyCode == KeyEvent.KEYCODE_MENU) { // Eat the event return true; } return false;} 

Aqui está uma solução com o ouvinte key. Não tenho ideia por que isso funciona, mas o OnKeyListener funciona se você simplesmente substitui onKeyPreIme em seu EditText personalizado.

SomeClass.java

 customEditText.setOnKeyListener((v, keyCode, event) -> { if(event.getAction() == KeyEvent.ACTION_DOWN) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: getPresenter().onBackPressed(); break; } } return false; }); } customEditText.setOnKeyListener((v, keyCode, event) -> { if(event.getAction() == KeyEvent.ACTION_DOWN) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: getPresenter().onBackPressed(); break; } } return false; }); } customEditText.setOnKeyListener((v, keyCode, event) -> { if(event.getAction() == KeyEvent.ACTION_DOWN) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: getPresenter().onBackPressed(); break; } } return false; }); retornair falso; customEditText.setOnKeyListener((v, keyCode, event) -> { if(event.getAction() == KeyEvent.ACTION_DOWN) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: getPresenter().onBackPressed(); break; } } return false; }); 

CustomEditText.java

 @Oviewride public boolean onKeyPreIme(int keyCode, KeyEvent event) { return super.dispatchKeyEvent(event); } 
Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.