Enter key on EditText hitting onKey twice

前端 未结 7 1874
暖寄归人
暖寄归人 2021-01-07 18:25

I\'ve attached an OnKeyListener to an EditText. I\'ve overrode the onKey handler to capture a KeyEvent.

When a user hits the enter key (either their computer enter

7条回答
  •  悲&欢浪女
    2021-01-07 19:27

    I debugged and what worked for me was that,

    editText.setOnKeyListener(View.OnKeyListener { view, i, keyEvent ->
                if (i == KeyEvent.KEYCODE_ENTER && enterAsSend && (keyEvent.action == KeyEvent.ACTION_UP || keyEvent.action == KeyEvent.ACTION_DOWN)) {
                    //Do stuff
                    }
                    return@OnKeyListener true
                }
                false
            })
    

    and checkout your Editext that android:inputType="textNoSuggestions" because the first click of enter key gives us the suggestion from the dictionary.

提交回复
热议问题