I have two EditText (each only only accepts one character) and I want to handle both fields like I had only one.
I'm using a TextWatcher to set the focus in the second one when the user writes a character in the first one, but I don't know how to do the opposite.
If the user press the delete button in the second EditText (being this EditText empty) I want to move the focus to the first EditText and delete the character there.
The problem is that TextWatcher doesn't work when the user tries to delete an empty field (because in fact nothing is changing). And onKeyDown event only works with hard keyboards so I don't have any idea of how to deal with this problem...
Thanks!
Many laptops add rows of smaller keys above the Function key line to add keys on a non-standard size keyboard. On this row of smaller keys, the position of the Delete key is positioned at or near the right-hand end.
The keyboard key used to delete the text character at, or to the right of, the screen cursor. Pressing Delete (DEL) also deletes the currently highlighted text, image or group of images. The Delete key removes characters to the right of the cursor, whereas the Backspace key deletes to the left. See Backspace key.
Possible duplicate of Android EditText delete(backspace) key event
just checked the code from that question (which actually come from the provided question and answered by Labeeb P) with the test project with just two edits on layout and it seems to work just fine - I'm able to receive delete even if edit is empty.
final EditText edit1 = (EditText) findViewById(R.id.editText1); edit1.setOnKeyListener(new View.OnKeyListener() { @Override 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 Log.e("IME_TEST", "DEL KEY"); } return false; } });
Seems android documentation of EditText should be made more clear or at least any guide for EditText - Soft Keyboard interaction provided, because there many typical ones whose should be worked out by nearly every developer.
UPDATE: Seems this way doesn't work on latest (at least after 4.1) Android versions. This answer seems to work on versions after 4.1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With