Hide Soft keyboard on return key press

前端 未结 9 1209
[愿得一人]
[愿得一人] 2020-12-29 19:00

I\'ve searched half a dozen other answers on SO, but haven\'t found one that works. All I\'m trying to do is dismiss the soft keyboard when the user presses the enter butto

9条回答
  •  被撕碎了的回忆
    2020-12-29 19:49

    This is what worked for me. Based on this layout xml:

    
    
        
    
    

    I needed to do this for the keyboard to dismiss and the text into to lose focus and hide the cursor.

    findViewById(R.id.ip_override_text_input_sat).setOnKeyListener { v, keyCode, event ->
                if (keyCode == KeyEvent.KEYCODE_ENTER && currentFocus != null) {
                    val inputManager =
                        getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                    inputManager.hideSoftInputFromWindow(
                        this.currentFocus!!.windowToken,
                        HIDE_NOT_ALWAYS
                    )
                    currentFocus!!.clearFocus()
                    return@setOnKeyListener true
                }
                return@setOnKeyListener false
            }
    

    Hope this helps someone doing the same

提交回复
热议问题