0

I have an Activity with two EditText fields. So when I press enter in the first one the second one becomes focused.

However I want to disable Enter key in the first EditText sometimes (for example when I think that the user haven't made a proper input into the first EditText).

I' ve overwriten onKeyDown for the 1st EditText returning true when key event is KEYCODE_ENTER but that doesn't help.

What shall I do?

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158

2 Answers2

0

Just by adding an editorActionListener:

editText1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                if(isValidContent(editText1.getText().toString()){
                    editText2.requestFocus();
                    return true;
                } else {
                       ....
                }
            }
            return false;
        }
    });
ldd
  • 440
  • 5
  • 9
0

How about adding an OnFocusChangeListener? When your EditText looses focus, you can do validation and can do a requestFocus for the "right" field.

Tom
  • 1,319
  • 9
  • 8
  • Yes, but if in onFocusChangeListener I set the focus back to my EditText that'll completely prevent me from leaving my EditText until I've made the 'correct' input. I don't really want that. Instead, in such case, I would like to disable Enter key only. Do you think I'd better not do this as this might contradict Android UI guidelines? – Alexander Kulyakhtin Oct 17 '11 at 20:38
  • Maybe this helps: http://stackoverflow.com/questions/1489852/android-handle-enter-in-an-edittext – Tom Oct 17 '11 at 20:49