EditText request focus not working

后端 未结 11 654
自闭症患者
自闭症患者 2020-12-15 17:03

I have an EditText and a Button. On click of the button i want to open the EditText keyboard and at the same time request focus on the

11条回答
  •  半阙折子戏
    2020-12-15 17:38

    I was combining some answers and found the following solution:

    fun Fragment.focusEditText(editText: EditText) {
    Timer("Timer", false).schedule(50) {
        requireActivity().runOnUiThread(java.lang.Runnable {
            editText.isFocusableInTouchMode = true
            editText.requestFocus()
            val manager =
                requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            manager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
        })
    }
    

    The timer delay makes sure that the focus request is working and the keyboard is opened manually because it did not work implicitely for me.

提交回复
热议问题