Android WebView HTML 输入按键不会触发

gab*_*ldi 4 javascript java android input webview

在某些设备(主要是三星,但也有其他设备)和以下设备的组合上:Android 版本、WebView 版本(即使是 Android 7 中的常青 WebView)和键盘,存在许多问题:

  • keypress 没有被解雇
  • keydown并且keyup总是包含keyCode=229
  • keypress并被input解雇但不包含密钥
  • textInput 没有被解雇
  • maxlengthinput[type=text]用户键入时不支持属性(maxlength允许超过字符,并且仅在提交表单时验证输入)

有没有办法解决这些问题?

gab*_*ldi 6

我发现如果你扩展WebView和覆盖onCreateInputConnection,所有这些问题都得到了解决:

public class WebViewExtended extends WebView {
    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        // This line fixes some issues but introduces others, YMMV.
        // super.onCreateInputConnection(outAttrs);

        return new BaseInputConnection(this, false);
    }
}
Run Code Online (Sandbox Code Playgroud)

覆盖之前onCreateInputConnection

前

覆盖onCreateInputConnectiong被按下)后:

后