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

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

I have a layout which contains some views like this:






&         


        
13条回答
  •  天命终不由人
    2020-11-28 02:31

    I tried the top answer by David Merriman and it also didn't work in my case. But I found the suggestion to run this code delayed here and it works like a charm.

    val editText = view.findViewById(R.id.settings_input_text)
    
    editText.postDelayed({
        editText.requestFocus()
    
        val imm = context.getSystemService(INPUT_METHOD_SERVICE) as? InputMethodManager
        imm?.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
    }, 100)
    

提交回复
热议问题