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.