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

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

I have a layout which contains some views like this:






&         


        
13条回答
  •  被撕碎了的回忆
    2020-11-28 02:31

    I tried a lot ways and it's not working tho, not sure is it because i'm using shared transition from fragment to activity containing the edit text.

    Btw my edittext is also wrapped in LinearLayout.

    I added a slight delay to request focus and below code worked for me: (Kotlin)

     et_search.postDelayed({
         editText.requestFocus()
    
         showKeyboard()
     },400) //only 400 is working fine, even 300 / 350, the cursor is not showing
    

    showKeyboard()

     val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
     imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
    

提交回复
热议问题