我已经覆盖了自定义 WebView 类中的 onCreateInputConnection ,如下所示
class CustomWebView extends WebView {
public CustomWebView(@Nullable Context context) {
super(context);
loadUrl("file:///android_asset/editor.html")
}
public CustomWebView(@Nullable Context context,@Nullable AttributeSet attrs) {
super(context, attrs);
loadUrl("file:///android_asset/editor.html")
}
public CustomWebView(@Nullable Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
loadUrl("file:///android_asset/editor.html")
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
// below return statement would be the default implementation
// return super.onCreateInputConnection(outAttrs)
return new BaseInputConnection(this, false);
}
}
一些可编辑的 HTML 内容被加载到上面的自定义 Webview 中,将下面的示例 editor.html 放在资产文件夹中
<html>
<head></head>
<body contenteditable="true">
<h1> this is a sample text </h1></br>
<h2> h2 text</h1>
</body>
</html>
在 WebView 中使用 BaseInputConnection 时的主要问题: 我无法使用键盘执行滑行输入或滑动字母输入。仅当使用 BaseInputConnection 而在默认实现中执行滑行输入时才会观察到此行为
WebView 中使用 BaseInputConnection 时的其他问题 在某些设备(例如 Redmi)中,使用软键盘键入时字母不会发送到 Webview。
我将它覆盖到 BaseInputConnection 因为它解决了我在其他用例中的许多问题。任何有关解决键盘问题的帮助将不胜感激