OK button of Android event keyboard

Good afternoon guys once again asking for help. I would like to know how I do to catch the OK button event from the keyboard. Example if I have a login and I want that when I click ok it has the same effect as the login button of my application would have.

Author: Rabelos, 2014-07-09

1 answers

I usually do this way:

<EditText
    // Demais atributos do seu EditText
    android:maxLines="1"
    android:lines="1"
    android:inputType="textImeMultiLine"
    android:imeActionLabel="@string/pronto"
    android:imeOptions="actionDone" />

Being:

  • maxLines and lines being 1, because otherwise it could overlap the line break button, so it can only have one line.
  • imeOptions the code referring to the Ok/Done button (in Kindle Fire they say it is actionGo, ai would have to change the check from actionId to EditorInfo.IME_ACTION_GO).
  • inputType needs to be textImeMultiline.
  • imeActionLabel the text that appears on the button.

In its Activity or Fragment, the treatment of the event should be like this:

EditText edit = findViewById(...);

edit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(event != null && KeyEvent.KEYCODE_ENTER == event.getKeyCode() || actionId == EditorInfo.IME_ACTION_DONE) {
            confirmAction();
        }

        return false;
    }
});

Just a remark, it seems to me that from experience this code only works for the standard keyboard of Google and Android. I couldn't get it to work on SwiftKey for example, they say that third-party applications don't respect the Ime Action button.

 6
Author: Wakim, 2014-07-09 23:22:23