How can I set the focus (and display the keyboard) on my EditText programmatically

后端 未结 13 864
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 02:22

I have a layout which contains some views like this:






&         


        
13条回答
  •  醉酒成梦
    2020-11-28 02:33

    editTxt.setOnFocusChangeListener { v, hasFocus ->
                val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                if (hasFocus) {
                    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
                } else {
                    imm.hideSoftInputFromWindow(v.windowToken, 0)
                }
            }
    

提交回复
热议问题