OnEditorActionListener not working

前端 未结 6 1352
心在旅途
心在旅途 2021-01-01 22:51

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!

6条回答
  •  青春惊慌失措
    2021-01-01 23:37

    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

    
    

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

提交回复
热议问题