5

I have 3 EditText elements, and I want to jump from one field to the next if there are 4 characters in the input.

I use a TextWatcher for this:

getEditView(R.id.edit_code1).addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {}

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void afterTextChanged(Editable input) {
        if(input.length() == 4){
            getEditView(R.id.edit_code2).requestFocus();
        }
    }
});

The inputType for the EditText is "textCapCharacters"

When doing a longpress on a key to get a number, like holding R to get a 4, most devices will not add the letter, until the user stops holding the button, and will fire afterTextChanged after the letter 4 is selected. On a HTC keyboard (In this case the HTC Desire on 2.3.5) this is not the case. Even though the user is still holding the button, the R is added to the EditText and afterTextChanged is fired, and the code does its job and puts the focus on the next field.

How can this undesired behavior be prevented? Watching onKeyDown/Up won't help, because it doesn't register normal keystrokes.

Jurriaanr
  • 53
  • 3

2 Answers2

2
       getEditView(R.id.edit_code1).addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
  if(input.length() >3){
        getEditView(R.id.edit_code2).requestFocus();
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

@Override
public void afterTextChanged(Editable input) {

    }
}

});

  • all 3 event listeners, onTextChanged, beforeTextChanged, afterTextChanged are fired as soon as a key is pressed, because than a letter is added to the edittext. The problem is that the implementation of the keyboard software doesn't wait for the user to release the key and select a number, before adding it, and calling all event listeners – Jurriaanr Mar 19 '12 at 12:23
1

Unfortunately it's a bug in the HTC keyboard with no way of fixing it. This being an event in the IME, it is impossible intercept touch or key events, so you would probably be better off simply making the user skip to the next input instead of you doing it for them.

If it's that important for you though, you can implement your own kind of keyboard there, with only the keys you need.

zrgiu
  • 6,200
  • 1
  • 33
  • 35
  • Thats what i was afraid of, thank you, I will leave the choice to the client. It is always possible on the HTC keyboards to use numeric input instead of a longpress anyway. – Jurriaanr Mar 21 '12 at 13:01