How to clear focus and remove keyboard on Android?

后端 未结 6 2153
情歌与酒
情歌与酒 2021-02-01 18:41

I have a EditText control. If I tap it the softkeyboard will popup however when I press \"enter/ok/return\" then the EditText control it still has focus and the keyboard up.

6条回答
  •  别跟我提以往
    2021-02-01 19:35

    Make sure your EditText XML has :

    android:id="@+id/myEditText"    
    android:imeOptions="actionDone"
    

    Then set listener to your EditText (with Kotlin, and from a fragment):

    myEditText.setOnEditorActionListener({ v, actionId, event ->
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    myEditText.clearFocus()
                    val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                    imm.hideSoftInputFromWindow(view!!.windowToken, 0)    
                }
                false
            })
    

提交回复
热议问题