onEditorAction() is not called after Enter key has been pressed on Jelly Bean emulator
Asked Answered
M

2

23

I'm having a problem with the behavior of the latest Jelly Bean emulator. I have several EditTexts in my app. An OnEditorActionListener provides special handling when a user presses the ENTER key on the keyboard. This worked up until ICS, but now on Jelly Bean the listener callback method onEditorAction() no longer gets called. Only a new line is inserted into the EditText.

This can be reproduced this way:

EditText testEditText = new EditText(context);
testEditText.setOnEditorActionListener(new OnEditorActionListener() {

    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        Log.d(TAG, "onEditorAction() called");
        return false;
    }
});
addView(testEditText);

Is this a bug in Jelly Bean? Or in the emulator? Or has the behavior been changed intentionally?

Curiously someone else writes that the method gets called, but with unexpected parameters, on a Nexus 7 running Jelly Bean here: null keyevent and actionid = 0 in onEditorAction() (Jelly Bean / Nexus 7)

Mendicity answered 3/7, 2012 at 13:17 Comment(3)
This sample project behaves as expected: github.com/commonsguy/cw-omnibus/tree/master/ActionBar/…Ari
@CommonsWare: Thank you, I found a workaround for my problem within your code. If I do the following, the ENTER key is exchanged with a GO key on the virtual keyboard, which triggers onEditorAction(): editText.setRawInputType(InputType.TYPE_CLASS_TEXT); editText.setImeOptions(EditorInfo.IME_ACTION_GO);Mendicity
Or in XML: android:imeOptions="actionGo" android:inputType="text"Mendicity
M
36

If someone else finds this question:

I've tested this several times and on the Jelly Bean emulator the listener callback method onEditorAction() indeed no longer gets called when the Enter key is pressed on the virtual keyboard.

As I mentioned above a possible solution or workaround is to replace the Enter key with one of the available action keys. Those still trigger onEditorAction(). I also had to specify the input type.

editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setImeOptions(EditorInfo.IME_ACTION_GO);
<EditText
...
android:imeOptions="actionGo"
android:inputType="text" />
Mendicity answered 18/7, 2012 at 13:1 Comment(1)
and what if the phone does not support imeActions at all? (There are many devices who does not.)Bruns
T
1

Here's what I did, which should cover all types of the Enter being pressed:

override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
    if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_NULL)
        ... // Enter pressed

In XML I added only android:imeOptions="actionGo"

The reason, according to the docs:

https://developer.android.com/reference/android/widget/TextView.OnEditorActionListener.html#onEditorAction(android.widget.TextView,%20int,%20android.view.KeyEvent)

actionId int: Identifier of the action. This will be either the identifier you supplied, or EditorInfo#IME_NULL if being called due to the enter key being pressed.

Tardiff answered 2/6, 2019 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.