Android Event Keyboard OK Button

1

Good afternoon, people again, asking for help. I would like to know how to get the OK button event from the keyboard. Example if I have a login and I want it to click on ok it will have the same effect that the button would enter from my application.

    
asked by anonymous 09.07.2014 / 22:05

1 answer

5

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

In your Activity or Fragment , the event handling should look 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 an observation, it seems to me from experience that this code only works for the standard keyboard of Google and Android . For example, I could not make SwiftKey work, say that third-party applications do not respect the Ime Action button.

    
10.07.2014 / 01:22