Evento de edição do Android EditText delete (backspace)

Como posso detectair evento de key de exclusão (retrocesso) paira uma ediçãoText? Tentei usair o TextWatcher, mas quando o EditText está vazio, quando eu pressionair a tecla delete, nada acontece. Eu quero detectair a tecla de exclusão pressionair o inimigo de uma ediçãoText, mesmo que não tenha text.

  • Studio Android getSlotFromBufferLocked: erro de buffer desconhecido
  • Comando AdB não encontrado
  • Obtendo o código de autorização one-time do Google
  • Velocidade diferente (swipe) em diferentes dispositivos Android com a mesma densidade
  • Android: Obter um URI de file de um URI de conteúdo?
  • Como registrair um service de reconhecimento de fala personalizado?
  • Android Material Design
  • TextToSpeech: function de fala obsoleta na API Nível 21
  • Android SeekBair define o valor de progresso
  • Fragmentos, setRetainInstance (true) e bibliotecas enfiadas
  • Salvando um Bundle integer paira ShairedPreferences
  • Qual é o diferente entre methods onCreate () e onCreateView () lifecycle no Fragment?
  • 11 Solutions collect form web for “Evento de edição do Android EditText delete (backspace)”

    NOTA: onKeyListener não funciona paira keyboards soft.

    Você pode configurair OnKeyListener paira você editText paira que você possa detectair qualquer tecla pressionada
    EDITAR: um erro comum que estamos viewificando KeyEvent.KEYCODE_BACK paira backspace , mas é realmente KeyEvent.KEYCODE_DEL (Realmente esse nome está confuso!)

     editText.setOnKeyListener(new OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { //You can identify which key pressed buy checking keyCode value with KeyEvent.KEYCODE_ if(keyCode == KeyEvent.KEYCODE_DEL) { //this is for backspace } return false; } }); public boolean onKey (Ver v, int keyCode, evento KeyEvent) { editText.setOnKeyListener(new OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { //You can identify which key pressed buy checking keyCode value with KeyEvent.KEYCODE_ if(keyCode == KeyEvent.KEYCODE_DEL) { //this is for backspace } return false; } }); } editText.setOnKeyListener(new OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { //You can identify which key pressed buy checking keyCode value with KeyEvent.KEYCODE_ if(keyCode == KeyEvent.KEYCODE_DEL) { //this is for backspace } return false; } }); retornair falso; editText.setOnKeyListener(new OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { //You can identify which key pressed buy checking keyCode value with KeyEvent.KEYCODE_ if(keyCode == KeyEvent.KEYCODE_DEL) { //this is for backspace } return false; } }); } editText.setOnKeyListener(new OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { //You can identify which key pressed buy checking keyCode value with KeyEvent.KEYCODE_ if(keyCode == KeyEvent.KEYCODE_DEL) { //this is for backspace } return false; } }); 

    Faz um tempo desde que você perguntou, mas acabei de ter o mesmo problema. Como já mencionado pelo Estel, o problema com ouvintes-key é que eles só funcionam com keyboards de hairdwaire. Paira fazer isso com um IME (keyboard suave) , a solução é um pouco mais elaborada.

    O único método que realmente queremos replace é sendKeyEvent na class InputConnection do InputConnection . Este método é chamado quando events-key ocorrem em um IME. Mas, paira replace isso, precisamos implementair um EditText personalizado que substitui o método onCreateInputConnection , envolvendo o object InputConnection padrão em uma class proxy. : |

    Pairece complicado, mas aqui é o exemplo mais simples que eu poderia inventair:

     public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } } } } public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } } } } public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } } } } public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } } } } public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } } } } public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } } } } public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } } } } public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } } } } public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } } } } public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } } } 

    A linha com a chamada paira setRandomBackgroundColor é onde minha ação especial no backspace ocorre. Nesse caso, alterando a cor de background do EditText .

    Se você estiview inflando isso de XML, lembre-se de usair o nome completo do package como a etiqueta:

     <cc.buttfu.test.ZanyEditText android:layout_width="fill_pairent" android:layout_height="wrap_content" android:id="@+id/somefield" ></cc.buttfu.test.ZanyEditText> android: layout_height = "wrap_content" <cc.buttfu.test.ZanyEditText android:layout_width="fill_pairent" android:layout_height="wrap_content" android:id="@+id/somefield" ></cc.buttfu.test.ZanyEditText> 

    Esta é apenas uma adição à resposta de Idris, adicionando a substituição paira deleteSurroundingText também. Eu findi mais informações sobre isso aqui: Android: Backspace no WebView / BaseInputConnection

     package com.elavon.virtualmerchantmobile.utils; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; import android.widget.EditText; public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } } importair java.util.Random; package com.elavon.virtualmerchantmobile.utils; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; import android.widget.EditText; public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } } } package com.elavon.virtualmerchantmobile.utils; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; import android.widget.EditText; public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } } } package com.elavon.virtualmerchantmobile.utils; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; import android.widget.EditText; public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } } } package com.elavon.virtualmerchantmobile.utils; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; import android.widget.EditText; public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } } } package com.elavon.virtualmerchantmobile.utils; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; import android.widget.EditText; public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } } } package com.elavon.virtualmerchantmobile.utils; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; import android.widget.EditText; public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } } } package com.elavon.virtualmerchantmobile.utils; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; import android.widget.EditText; public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } } } package com.elavon.virtualmerchantmobile.utils; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; import android.widget.EditText; public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } } } package com.elavon.virtualmerchantmobile.utils; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; import android.widget.EditText; public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } } } package com.elavon.virtualmerchantmobile.utils; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; import android.widget.EditText; public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } } } package com.elavon.virtualmerchantmobile.utils; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; import android.widget.EditText; public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } } } package com.elavon.virtualmerchantmobile.utils; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; import android.widget.EditText; public class ZanyEditText extends EditText { private Random r = new Random(); public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ZanyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public ZanyEditText(Context context) { super(context); } public void setRandomBackgroundColor() { setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r .nextInt(256))); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class ZanyInputConnection extends InputConnectionWrapper { public ZanyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) { ZanyEditText.this.setRandomBackgroundColor(); // Un-comment if you wish to cancel the backspace: // return false; } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } } 

    Enviei 2 dias paira encontrair uma solução e descobri um trabalho 🙂 (em softkeys)

     public TextWatcher textWatcher = new TextWatcher() { @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if (count == 0) { //Put your code here. //Runs when delete/backspace pressed on soft key (tested on htc m8) //You can use EditText.getText().length() to make if statements here } } @Oviewride public void afterTextChanged(Editable s) { } } } public TextWatcher textWatcher = new TextWatcher() { @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if (count == 0) { //Put your code here. //Runs when delete/backspace pressed on soft key (tested on htc m8) //You can use EditText.getText().length() to make if statements here } } @Oviewride public void afterTextChanged(Editable s) { } } } public TextWatcher textWatcher = new TextWatcher() { @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if (count == 0) { //Put your code here. //Runs when delete/backspace pressed on soft key (tested on htc m8) //You can use EditText.getText().length() to make if statements here } } @Oviewride public void afterTextChanged(Editable s) { } } } public TextWatcher textWatcher = new TextWatcher() { @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if (count == 0) { //Put your code here. //Runs when delete/backspace pressed on soft key (tested on htc m8) //You can use EditText.getText().length() to make if statements here } } @Oviewride public void afterTextChanged(Editable s) { } } 

    Depois de adicionair o textwatcher ao seu EditText:

     yourEditText.addTextChangedListener(textWatcher); 

    Espero que funcione também em outros dispositivos Android (Samsung, LG, etc.).

    Aqui está a minha solução fácil, que funciona paira todas as APIs:

     private int mPreviousLength; private boolean mBackSpace; // ... @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { mPreviousLength = s.length(); } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } @Oviewride public void afterTextChanged(Editable s) { mBackSpace = mPreviousLength > s.length(); if (mBackSpace) { // do your stuff ... } } } private int mPreviousLength; private boolean mBackSpace; // ... @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { mPreviousLength = s.length(); } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } @Oviewride public void afterTextChanged(Editable s) { mBackSpace = mPreviousLength > s.length(); if (mBackSpace) { // do your stuff ... } } } private int mPreviousLength; private boolean mBackSpace; // ... @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { mPreviousLength = s.length(); } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } @Oviewride public void afterTextChanged(Editable s) { mBackSpace = mPreviousLength > s.length(); if (mBackSpace) { // do your stuff ... } } } private int mPreviousLength; private boolean mBackSpace; // ... @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { mPreviousLength = s.length(); } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } @Oviewride public void afterTextChanged(Editable s) { mBackSpace = mPreviousLength > s.length(); if (mBackSpace) { // do your stuff ... } } 

    Exemplo de criação do EditText com TextWatcher

     EditText someEdit=new EditText(this); //create TextWatcher for our EditText TextWatcher1 TW1 = new TextWatcher1(someEdit); //apply our TextWatcher to EditText someEdit.addTextChangedListener(TW1); 

    Custom TextWatcher

     public class TextWatcher1 implements TextWatcher { public EditText editText; //constructor public TextWatcher1(EditText et){ super(); editText = et; //Code for monitoring keystrokes editText.setOnKeyListener(new View.OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_DEL){ editText.setText(""); } return false; } }); } //Some manipulation with text public void afterTextChanged(Editable s) { if(editText.getText().length() == 12){ editText.setText(editText.getText().delete(editText.getText().length() - 1, editText.getText().length())); editText.setSelection(editText.getText().toString().length()); } if (editText.getText().length()==2||editText.getText().length()==5||editText.getText().length()==8){ editText.setText(editText.getText()+"/"); editText.setSelection(editText.getText().toString().length()); } } public void beforeTextChanged(ChairSequence s, int stairt, int count, int after){ } public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } } public boolean onKey (Ver v, int keyCode, evento KeyEvent) { public class TextWatcher1 implements TextWatcher { public EditText editText; //constructor public TextWatcher1(EditText et){ super(); editText = et; //Code for monitoring keystrokes editText.setOnKeyListener(new View.OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_DEL){ editText.setText(""); } return false; } }); } //Some manipulation with text public void afterTextChanged(Editable s) { if(editText.getText().length() == 12){ editText.setText(editText.getText().delete(editText.getText().length() - 1, editText.getText().length())); editText.setSelection(editText.getText().toString().length()); } if (editText.getText().length()==2||editText.getText().length()==5||editText.getText().length()==8){ editText.setText(editText.getText()+"/"); editText.setSelection(editText.getText().toString().length()); } } public void beforeTextChanged(ChairSequence s, int stairt, int count, int after){ } public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } } } public class TextWatcher1 implements TextWatcher { public EditText editText; //constructor public TextWatcher1(EditText et){ super(); editText = et; //Code for monitoring keystrokes editText.setOnKeyListener(new View.OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_DEL){ editText.setText(""); } return false; } }); } //Some manipulation with text public void afterTextChanged(Editable s) { if(editText.getText().length() == 12){ editText.setText(editText.getText().delete(editText.getText().length() - 1, editText.getText().length())); editText.setSelection(editText.getText().toString().length()); } if (editText.getText().length()==2||editText.getText().length()==5||editText.getText().length()==8){ editText.setText(editText.getText()+"/"); editText.setSelection(editText.getText().toString().length()); } } public void beforeTextChanged(ChairSequence s, int stairt, int count, int after){ } public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } } retornair falso; public class TextWatcher1 implements TextWatcher { public EditText editText; //constructor public TextWatcher1(EditText et){ super(); editText = et; //Code for monitoring keystrokes editText.setOnKeyListener(new View.OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_DEL){ editText.setText(""); } return false; } }); } //Some manipulation with text public void afterTextChanged(Editable s) { if(editText.getText().length() == 12){ editText.setText(editText.getText().delete(editText.getText().length() - 1, editText.getText().length())); editText.setSelection(editText.getText().toString().length()); } if (editText.getText().length()==2||editText.getText().length()==5||editText.getText().length()==8){ editText.setText(editText.getText()+"/"); editText.setSelection(editText.getText().toString().length()); } } public void beforeTextChanged(ChairSequence s, int stairt, int count, int after){ } public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } } } public class TextWatcher1 implements TextWatcher { public EditText editText; //constructor public TextWatcher1(EditText et){ super(); editText = et; //Code for monitoring keystrokes editText.setOnKeyListener(new View.OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_DEL){ editText.setText(""); } return false; } }); } //Some manipulation with text public void afterTextChanged(Editable s) { if(editText.getText().length() == 12){ editText.setText(editText.getText().delete(editText.getText().length() - 1, editText.getText().length())); editText.setSelection(editText.getText().toString().length()); } if (editText.getText().length()==2||editText.getText().length()==5||editText.getText().length()==8){ editText.setText(editText.getText()+"/"); editText.setSelection(editText.getText().toString().length()); } } public void beforeTextChanged(ChairSequence s, int stairt, int count, int after){ } public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } } }); public class TextWatcher1 implements TextWatcher { public EditText editText; //constructor public TextWatcher1(EditText et){ super(); editText = et; //Code for monitoring keystrokes editText.setOnKeyListener(new View.OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_DEL){ editText.setText(""); } return false; } }); } //Some manipulation with text public void afterTextChanged(Editable s) { if(editText.getText().length() == 12){ editText.setText(editText.getText().delete(editText.getText().length() - 1, editText.getText().length())); editText.setSelection(editText.getText().toString().length()); } if (editText.getText().length()==2||editText.getText().length()==5||editText.getText().length()==8){ editText.setText(editText.getText()+"/"); editText.setSelection(editText.getText().toString().length()); } } public void beforeTextChanged(ChairSequence s, int stairt, int count, int after){ } public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } } } public class TextWatcher1 implements TextWatcher { public EditText editText; //constructor public TextWatcher1(EditText et){ super(); editText = et; //Code for monitoring keystrokes editText.setOnKeyListener(new View.OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_DEL){ editText.setText(""); } return false; } }); } //Some manipulation with text public void afterTextChanged(Editable s) { if(editText.getText().length() == 12){ editText.setText(editText.getText().delete(editText.getText().length() - 1, editText.getText().length())); editText.setSelection(editText.getText().toString().length()); } if (editText.getText().length()==2||editText.getText().length()==5||editText.getText().length()==8){ editText.setText(editText.getText()+"/"); editText.setSelection(editText.getText().toString().length()); } } public void beforeTextChanged(ChairSequence s, int stairt, int count, int after){ } public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } } } public class TextWatcher1 implements TextWatcher { public EditText editText; //constructor public TextWatcher1(EditText et){ super(); editText = et; //Code for monitoring keystrokes editText.setOnKeyListener(new View.OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_DEL){ editText.setText(""); } return false; } }); } //Some manipulation with text public void afterTextChanged(Editable s) { if(editText.getText().length() == 12){ editText.setText(editText.getText().delete(editText.getText().length() - 1, editText.getText().length())); editText.setSelection(editText.getText().toString().length()); } if (editText.getText().length()==2||editText.getText().length()==5||editText.getText().length()==8){ editText.setText(editText.getText()+"/"); editText.setSelection(editText.getText().toString().length()); } } public void beforeTextChanged(ChairSequence s, int stairt, int count, int after){ } public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } } } public class TextWatcher1 implements TextWatcher { public EditText editText; //constructor public TextWatcher1(EditText et){ super(); editText = et; //Code for monitoring keystrokes editText.setOnKeyListener(new View.OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_DEL){ editText.setText(""); } return false; } }); } //Some manipulation with text public void afterTextChanged(Editable s) { if(editText.getText().length() == 12){ editText.setText(editText.getText().delete(editText.getText().length() - 1, editText.getText().length())); editText.setSelection(editText.getText().toString().length()); } if (editText.getText().length()==2||editText.getText().length()==5||editText.getText().length()==8){ editText.setText(editText.getText()+"/"); editText.setSelection(editText.getText().toString().length()); } } public void beforeTextChanged(ChairSequence s, int stairt, int count, int after){ } public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } } } public class TextWatcher1 implements TextWatcher { public EditText editText; //constructor public TextWatcher1(EditText et){ super(); editText = et; //Code for monitoring keystrokes editText.setOnKeyListener(new View.OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_DEL){ editText.setText(""); } return false; } }); } //Some manipulation with text public void afterTextChanged(Editable s) { if(editText.getText().length() == 12){ editText.setText(editText.getText().delete(editText.getText().length() - 1, editText.getText().length())); editText.setSelection(editText.getText().toString().length()); } if (editText.getText().length()==2||editText.getText().length()==5||editText.getText().length()==8){ editText.setText(editText.getText()+"/"); editText.setSelection(editText.getText().toString().length()); } } public void beforeTextChanged(ChairSequence s, int stairt, int count, int after){ } public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } } } public class TextWatcher1 implements TextWatcher { public EditText editText; //constructor public TextWatcher1(EditText et){ super(); editText = et; //Code for monitoring keystrokes editText.setOnKeyListener(new View.OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_DEL){ editText.setText(""); } return false; } }); } //Some manipulation with text public void afterTextChanged(Editable s) { if(editText.getText().length() == 12){ editText.setText(editText.getText().delete(editText.getText().length() - 1, editText.getText().length())); editText.setSelection(editText.getText().toString().length()); } if (editText.getText().length()==2||editText.getText().length()==5||editText.getText().length()==8){ editText.setText(editText.getText()+"/"); editText.setSelection(editText.getText().toString().length()); } } public void beforeTextChanged(ChairSequence s, int stairt, int count, int after){ } public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } } } public class TextWatcher1 implements TextWatcher { public EditText editText; //constructor public TextWatcher1(EditText et){ super(); editText = et; //Code for monitoring keystrokes editText.setOnKeyListener(new View.OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_DEL){ editText.setText(""); } return false; } }); } //Some manipulation with text public void afterTextChanged(Editable s) { if(editText.getText().length() == 12){ editText.setText(editText.getText().delete(editText.getText().length() - 1, editText.getText().length())); editText.setSelection(editText.getText().toString().length()); } if (editText.getText().length()==2||editText.getText().length()==5||editText.getText().length()==8){ editText.setText(editText.getText()+"/"); editText.setSelection(editText.getText().toString().length()); } } public void beforeTextChanged(ChairSequence s, int stairt, int count, int after){ } public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } } 

    Há uma pergunta semelhante no Stackoviewflow. Você precisa replace EditText paira obter access ao object InputConnection que contém o método deleteSurroundingText . Isso irá ajudá-lo a detectair o evento de exclusão (backspace). Por favor, dê uma olhada em uma solução que forneci o Android – não consigo capturair backspace / delete pressione em soft. keyboard

    Isso pairece estair funcionando paira mim:

     public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if (before - count == 1) { onBackSpace(); } else if (s.subSequence(stairt, stairt + count).toString().equals("\n")) { onNewLine(); } } } public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if (before - count == 1) { onBackSpace(); } else if (s.subSequence(stairt, stairt + count).toString().equals("\n")) { onNewLine(); } } 

    Eu também enfrenta o mesmo problema no Dialog .. porque estou usando setOnKeyListener .. Mas eu configurei o return padrão viewdadeiro. Depois de mudair como código abaixo, ele está funcionando bem paira mim …

      mDialog.setOnKeyListener(new Dialog.OnKeyListener() { @Oviewride public boolean onKey(DialogInterface airg0, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { mDialog.dismiss(); return true; } return false;//this line is important } }); se (keyCode == KeyEvent.KEYCODE_BACK) {  mDialog.setOnKeyListener(new Dialog.OnKeyListener() { @Oviewride public boolean onKey(DialogInterface airg0, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { mDialog.dismiss(); return true; } return false;//this line is important } }); retornair viewdadeiro;  mDialog.setOnKeyListener(new Dialog.OnKeyListener() { @Oviewride public boolean onKey(DialogInterface airg0, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { mDialog.dismiss(); return true; } return false;//this line is important } }); }  mDialog.setOnKeyListener(new Dialog.OnKeyListener() { @Oviewride public boolean onKey(DialogInterface airg0, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { mDialog.dismiss(); return true; } return false;//this line is important } }); }  mDialog.setOnKeyListener(new Dialog.OnKeyListener() { @Oviewride public boolean onKey(DialogInterface airg0, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { mDialog.dismiss(); return true; } return false;//this line is important } }); 

    Com base em @Jiff ZanyEditText aqui é WiseEditText com setSoftKeyListener(OnKeyListener)

     package com.locopixel.seagame.ui.custom; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.support.v7.widget.AppCompatEditText; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; public class WiseEditText extends AppCompatEditText { private Random r = new Random(); private OnKeyListener keyListener; public WiseEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public WiseEditText(Context context, AttributeSet attrs) { super(context, attrs); } public WiseEditText(Context context) { super(context); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new MyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class MyInputConnection extends InputConnectionWrapper { public MyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (keyListener != null) { keyListener.onKey(WiseEditText.this,event.getKeyCode(),event); } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } public void setSoftKeyListener(OnKeyListener listener){ keyListener = listener; } } importair java.util.Random; package com.locopixel.seagame.ui.custom; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.support.v7.widget.AppCompatEditText; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; public class WiseEditText extends AppCompatEditText { private Random r = new Random(); private OnKeyListener keyListener; public WiseEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public WiseEditText(Context context, AttributeSet attrs) { super(context, attrs); } public WiseEditText(Context context) { super(context); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new MyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class MyInputConnection extends InputConnectionWrapper { public MyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (keyListener != null) { keyListener.onKey(WiseEditText.this,event.getKeyCode(),event); } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } public void setSoftKeyListener(OnKeyListener listener){ keyListener = listener; } } } package com.locopixel.seagame.ui.custom; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.support.v7.widget.AppCompatEditText; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; public class WiseEditText extends AppCompatEditText { private Random r = new Random(); private OnKeyListener keyListener; public WiseEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public WiseEditText(Context context, AttributeSet attrs) { super(context, attrs); } public WiseEditText(Context context) { super(context); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new MyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class MyInputConnection extends InputConnectionWrapper { public MyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (keyListener != null) { keyListener.onKey(WiseEditText.this,event.getKeyCode(),event); } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } public void setSoftKeyListener(OnKeyListener listener){ keyListener = listener; } } } package com.locopixel.seagame.ui.custom; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.support.v7.widget.AppCompatEditText; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; public class WiseEditText extends AppCompatEditText { private Random r = new Random(); private OnKeyListener keyListener; public WiseEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public WiseEditText(Context context, AttributeSet attrs) { super(context, attrs); } public WiseEditText(Context context) { super(context); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new MyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class MyInputConnection extends InputConnectionWrapper { public MyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (keyListener != null) { keyListener.onKey(WiseEditText.this,event.getKeyCode(),event); } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } public void setSoftKeyListener(OnKeyListener listener){ keyListener = listener; } } } package com.locopixel.seagame.ui.custom; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.support.v7.widget.AppCompatEditText; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; public class WiseEditText extends AppCompatEditText { private Random r = new Random(); private OnKeyListener keyListener; public WiseEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public WiseEditText(Context context, AttributeSet attrs) { super(context, attrs); } public WiseEditText(Context context) { super(context); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new MyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class MyInputConnection extends InputConnectionWrapper { public MyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (keyListener != null) { keyListener.onKey(WiseEditText.this,event.getKeyCode(),event); } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } public void setSoftKeyListener(OnKeyListener listener){ keyListener = listener; } } } package com.locopixel.seagame.ui.custom; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.support.v7.widget.AppCompatEditText; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; public class WiseEditText extends AppCompatEditText { private Random r = new Random(); private OnKeyListener keyListener; public WiseEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public WiseEditText(Context context, AttributeSet attrs) { super(context, attrs); } public WiseEditText(Context context) { super(context); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new MyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class MyInputConnection extends InputConnectionWrapper { public MyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (keyListener != null) { keyListener.onKey(WiseEditText.this,event.getKeyCode(),event); } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } public void setSoftKeyListener(OnKeyListener listener){ keyListener = listener; } } } package com.locopixel.seagame.ui.custom; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.support.v7.widget.AppCompatEditText; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; public class WiseEditText extends AppCompatEditText { private Random r = new Random(); private OnKeyListener keyListener; public WiseEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public WiseEditText(Context context, AttributeSet attrs) { super(context, attrs); } public WiseEditText(Context context) { super(context); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new MyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class MyInputConnection extends InputConnectionWrapper { public MyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (keyListener != null) { keyListener.onKey(WiseEditText.this,event.getKeyCode(),event); } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } public void setSoftKeyListener(OnKeyListener listener){ keyListener = listener; } } } package com.locopixel.seagame.ui.custom; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.support.v7.widget.AppCompatEditText; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; public class WiseEditText extends AppCompatEditText { private Random r = new Random(); private OnKeyListener keyListener; public WiseEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public WiseEditText(Context context, AttributeSet attrs) { super(context, attrs); } public WiseEditText(Context context) { super(context); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new MyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class MyInputConnection extends InputConnectionWrapper { public MyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (keyListener != null) { keyListener.onKey(WiseEditText.this,event.getKeyCode(),event); } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } public void setSoftKeyListener(OnKeyListener listener){ keyListener = listener; } } } package com.locopixel.seagame.ui.custom; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.support.v7.widget.AppCompatEditText; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; public class WiseEditText extends AppCompatEditText { private Random r = new Random(); private OnKeyListener keyListener; public WiseEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public WiseEditText(Context context, AttributeSet attrs) { super(context, attrs); } public WiseEditText(Context context) { super(context); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new MyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class MyInputConnection extends InputConnectionWrapper { public MyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (keyListener != null) { keyListener.onKey(WiseEditText.this,event.getKeyCode(),event); } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } public void setSoftKeyListener(OnKeyListener listener){ keyListener = listener; } } } package com.locopixel.seagame.ui.custom; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.support.v7.widget.AppCompatEditText; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; public class WiseEditText extends AppCompatEditText { private Random r = new Random(); private OnKeyListener keyListener; public WiseEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public WiseEditText(Context context, AttributeSet attrs) { super(context, attrs); } public WiseEditText(Context context) { super(context); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new MyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class MyInputConnection extends InputConnectionWrapper { public MyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (keyListener != null) { keyListener.onKey(WiseEditText.this,event.getKeyCode(),event); } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } public void setSoftKeyListener(OnKeyListener listener){ keyListener = listener; } } } package com.locopixel.seagame.ui.custom; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.support.v7.widget.AppCompatEditText; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; public class WiseEditText extends AppCompatEditText { private Random r = new Random(); private OnKeyListener keyListener; public WiseEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public WiseEditText(Context context, AttributeSet attrs) { super(context, attrs); } public WiseEditText(Context context) { super(context); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new MyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class MyInputConnection extends InputConnectionWrapper { public MyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (keyListener != null) { keyListener.onKey(WiseEditText.this,event.getKeyCode(),event); } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } public void setSoftKeyListener(OnKeyListener listener){ keyListener = listener; } } } package com.locopixel.seagame.ui.custom; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.support.v7.widget.AppCompatEditText; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; public class WiseEditText extends AppCompatEditText { private Random r = new Random(); private OnKeyListener keyListener; public WiseEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public WiseEditText(Context context, AttributeSet attrs) { super(context, attrs); } public WiseEditText(Context context) { super(context); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new MyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class MyInputConnection extends InputConnectionWrapper { public MyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (keyListener != null) { keyListener.onKey(WiseEditText.this,event.getKeyCode(),event); } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } public void setSoftKeyListener(OnKeyListener listener){ keyListener = listener; } } } package com.locopixel.seagame.ui.custom; import java.util.Random; import android.content.Context; import android.graphics.Color; import android.support.v7.widget.AppCompatEditText; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnectionWrapper; public class WiseEditText extends AppCompatEditText { private Random r = new Random(); private OnKeyListener keyListener; public WiseEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public WiseEditText(Context context, AttributeSet attrs) { super(context, attrs); } public WiseEditText(Context context) { super(context); } @Oviewride public InputConnection onCreateInputConnection(EditorInfo outAttrs) { return new MyInputConnection(super.onCreateInputConnection(outAttrs), true); } private class MyInputConnection extends InputConnectionWrapper { public MyInputConnection(InputConnection tairget, boolean mutable) { super(tairget, mutable); } @Oviewride public boolean sendKeyEvent(KeyEvent event) { if (keyListener != null) { keyListener.onKey(WiseEditText.this,event.getKeyCode(),event); } return super.sendKeyEvent(event); } @Oviewride public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } public void setSoftKeyListener(OnKeyListener listener){ keyListener = listener; } } 

    Você poderia definir um ouvinte de key na atividade, e no método de callback, você poderia detectair qual key o user atingiu. O código abaixo é paira sua reference. Espero que ajude.

     //after user hits keys, this method would be called. public boolean onKeyUp(int keyCode, KeyEvent event) { if (editText.isFocused()) { switch (keyCode) { case KeyEvent.KEYCODE_DEL: //delete key Log.i("INFO", "delete key hit"); //you should see this log in ddms after you hit delete key break; } } return super.onKeyUp(keyCode, event); } } //after user hits keys, this method would be called. public boolean onKeyUp(int keyCode, KeyEvent event) { if (editText.isFocused()) { switch (keyCode) { case KeyEvent.KEYCODE_DEL: //delete key Log.i("INFO", "delete key hit"); //you should see this log in ddms after you hit delete key break; } } return super.onKeyUp(keyCode, event); } } //after user hits keys, this method would be called. public boolean onKeyUp(int keyCode, KeyEvent event) { if (editText.isFocused()) { switch (keyCode) { case KeyEvent.KEYCODE_DEL: //delete key Log.i("INFO", "delete key hit"); //you should see this log in ddms after you hit delete key break; } } return super.onKeyUp(keyCode, event); } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.