Android setOnEditorActionListener() doesn't fi

2020-02-12 02:08发布

I'm trying to set a listener to EditText when enter button will be pressed.But it didn't fire at all. I tested this on LG Nexus 4 with Android 4.2.2. setOnEditorActionListener works on Amazon Kindle Fire with Android 2.3 and setImeActionLabel works nowhere! I also can't set text for Enter button.Here is code:

mEditText.setImeActionLabel("Reply", EditorInfo.IME_ACTION_UNSPECIFIED);
mEditText.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
            Log.d("TEST RESPONSE", "Action ID = " + actionId + "KeyEvent = " + event);
            return true;
        }  
    });

What am I doing wrong? How can I fix this?

标签: java android ime
9条回答
唯我独甜
2楼-- · 2020-02-12 03:00

What worked for me is this,I have added this below line to EditText

android:imeOptions="actionSend"

this line makes keyboard which pops up when clicked on edit text has send button instead of search

in the setOnEditorActionListener you override following method looking for action send

@Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        if (actionId == EditorInfo.IME_ACTION_SEND) {
        //implement stuff here
          }
        }
查看更多
祖国的老花朵
3楼-- · 2020-02-12 03:02

You can try this to use the OnKeyListener

editText.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int actionId, KeyEvent event) {
            Log.d("TEST RESPONSE", "Action ID = " + actionId + "KeyEvent = " + event);
            return false;
        }
    });
查看更多
虎瘦雄心在
4楼-- · 2020-02-12 03:06

In the xml file add tag android:inputType="text" to the EditText

查看更多
登录 后发表回答