记录HarmonyOS 3.0.0键盘弹出失败

384 阅读1分钟

场景: 在webview中有一个输入框,在进入activity时需要判断内容为空时,进行弹出软键盘提供给用户快捷输入文案。按正常机型是用以下代码即可弹出键盘

val inputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
val view = currentFocus ?: return
val binder = view.windowToken ?: return
inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS)

奈何在HarmonyOS 3.0.0会有一条以下log出现,不进行弹出键盘 Ignoring showSoftInput() as view=xxx.xxx.xxx.WebView{2f367ae V.E...... ........ 0,0-1228,2320 #7f0a01e4 app:id/webview} is not served. Current served view is xxx.xxx.xxx.xxView{20b6a55 VFE...... .F...... 0,0-1228,2320 aid=1073741825} 需要使用当前有focus的view去show可以出来

val inputMethodManager =
    getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(
    window.currentFocus, 0,
)