InputMethodManager#showSoftInput() 方法彈出軟鍵盤(pán)需要EditText完全繪制完畢后執(zhí)行才能生效。
可以簡(jiǎn)單的設(shè)置一個(gè)延遲時(shí)間悴品,Kotlin代碼如下:
fun EditText.showSoftInput() {
this.requestFocus()
this.postDelayed({
val inputManager =
this.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.showSoftInput(this, 0)
}, 200)
}
使用時(shí)直接調(diào)用即可
editText.showSoftInput()
或者直接使用utilcodex中的工具類
import com.blankj.utilcode.util.KeyboardUtils
KeyboardUtils.showSoftInput(editText)