OnEditorActionListener not working

纵然是瞬间 提交于 2019-12-21 03:44:29

问题


I simply want to catch the event when the user press enter on an editText.

I did not get the Toast message, not the "Enter pressed" and not the "Some key pressed!" either.

What m i doing wrong?

myEditText.setOnEditorActionListener(new OnEditorActionListener() {

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

   Toast.makeText(getApplicationContext(), "Some key pressed!", Toast.LENGTH_LONG).show();

        if (event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
             Toast.makeText(getApplicationContext(), "Enter pressed", Toast.LENGTH_LONG).show();
                return true;
                }
                return false;
            }
        });

E D I T :

Well it is working on Android 2.3.3 and not working on 4.1.2 Any ideas how can i make this work on any android device?


回答1:


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.




回答2:


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




回答3:


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




回答4:


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.




回答5:


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




回答6:


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).



来源:https://stackoverflow.com/questions/15901863/oneditoractionlistener-not-working

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