OnEditorActionListener not working

大憨熊 提交于 2019-12-03 11:03:53
Yang Peiyong

Please try to set the EditText in single line mode.

editText.setSingleLine();

or in your xml layout file:

android:singleLine="true"

UPDATE

android:singleLine="true" is deprecated. From now on, use android:maxLines="1" with android:inputType="text" for achieving this behaviour

OR

editText.setMaxLines(1);
editText.setInputType(InputType.TYPE_CLASS_TEXT);

for setting programmatically.

use android:imeOptions="actionGo" in xml. actionDone no longer responds to onEditorAction.

Atul Chaturvedi

android:inputType="text" and android:imeOptions="actionGo" do the wonder.

myEditText.setOnEditorActionListener(new OnEditorActionListener() {

public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
    Toast.makeText(getApplicationContext(), "some key pressed", Toast.LENGTH_LONG)
                        .show();
    return false;
}
});

This code displays me some key pressed when pressed enter key after typing something in my edittext. But I don't what is problem in your code.

Gerrerth

The problem for me was that I had set in XML android:inputType="textMultiline". When I removed textMultiline option, the EditorListener started working.

I don't know why Atul Chatuverdi's answer was downvoted, -- after lots of browsing, this one finally did "a wonder" to me.

I confirm that ActionDone may not work. It works on my Samsung but for some reason doesn't work on Fly S. The event is just not fired. Both are Android 7.1. I wonder, if it is a bug of Google's keyboard...

The code that I used to make it finally work on all devices is

<EditText   
     android:singleLine="true"
     android:inputType="textUri"
     android:maxLines="1"
     android:imeOptions="actionGo"          
     ...
     android:id="@+id/et_path"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
    />

(You can use text instead of textUri, depending on your needs).

In my fragment:

editText = view.findViewById(R.id.et_path);
...
editText.setOnEditorActionListener(new onGo());

and the nested class to process the action.

private class onGo implements TextView.OnEditorActionListener {
   @Override
   public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
    if (i == EditorInfo.IME_ACTION_GO) {

         // To do stuff
        return true;
    }
    return false;
   }
  }

As for if (event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) in a multiline text, I have the same problem. It works with Samsung keyboard, Hacker's keyboard, but not Google keyboard.

Advice to use a textwatcher is a no-go for various reasons (like pasting text with enter signs instead of pressing the enter button and others).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!