Android中EditText动态获取焦点并弹出软键盘

2020-08-05  本文已影响0人  带带大湿兄

InputMethodManager#showSoftInput() 方法弹出软键盘需要EditText完全绘制完毕后执行才能生效。

可以简单的设置一个延迟时间,Kotlin代码如下:

fun EditText.showSoftInput() {
    this.requestFocus()
    this.postDelayed({
        val inputManager =
            this.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputManager.showSoftInput(this, 0)
    }, 200)
}

使用时直接调用即可

editText.showSoftInput()

或者直接使用utilcodex中的工具类

import com.blankj.utilcode.util.KeyboardUtils

KeyboardUtils.showSoftInput(editText)
上一篇 下一篇

猜你喜欢

热点阅读