I have 6 edit texts on my screen (used to enter 6 digits) If I enter 1 digit on one EditText field the focus should go onto the next one and so on. If I hit back space on some edit text field the focus should come to the previous edittext field
For this I used the below code
EditText2.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (EditText2.getText().toString().length() == 1) {
EditText3.requestFocus();
}
else if (EditText2.getText().toString().length() == 0) {
EditText1.requestFocus();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s) {
}
});
Normally , the ontextchange listener works if I don't hit a backspace on any of the edit texts. If I hit backspace on EditText2 and land on EditText1(some value on it is present) and try to enter something on it. The cursor is not going to EditText2 as addTextChangedListener not working.
Any help is greatly appreciated.
I'm assuming that you've probably set your
EditText
fields up with amaxLength
of 1. Something like this:When your
maxLength
is set to 1, theTextWatcher
will not be triggered when trying to enter additional characters. One way to solve your problem is when moving backwards to useeditText.setSelection(0, editText.length());
See below for a working example: