OK button of the Android Keyboard event

Asked

Viewed 3,345 times

1

Good afternoon guys again asking for help. I wonder how to pick up the event from the OK button on the keyboard. Example if I have a login and want to click ok it has the same effect as the login button of my application.

1 answer

6


I usually do it that 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 override the line break button, so it can only have one line.
  • imeOptions the Ok/Done button code (On Kindle Fire they say it is actionGo, then I would have to change the check of the actionId for EditorInfo.IME_ACTION_GO).
  • inputType needs to be textImeMultiline.
  • imeActionLabel the text that appears on the button.

In his Activity or Fragment, the treatment of the event should be in this way:

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 one observation, it seems to me that by own experience this code only works for the standard keyboard of Google and of Android. I couldn’t get it to work on Swiftkey for example, they say that third-party appliances do not respect the Ime Action.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.