无法使用 InputConnection 在 Android WebView 上的 contenteditable 中删除 div视频、图像、自定义 div

问题描述

我从 3 天开始一​​直在努力将图像、视频或自定义 div 块删除到 WebView 中的内容可编辑 div 中,我正在使用富内容编辑器,并且使用的是 Android 11

我已经阅读了很多关于使用 InputConnection 之类的文章/帖子

此刻我拥有的是:

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    outAttrs.inputType = InputType.TYPE_NULL;
    outAttrs.imeOptions = EditorInfo.IME_ACTION_NONE;
    final InputConnection con = new BaseInputConnection(this,true);
    InputConnectionWrapper public_con = new InputConnectionWrapper(con,true) {
        @Override
        public boolean deleteSurroundingText(int beforeLength,int afterLength) {
            if (beforeLength == 1 && afterLength == 0) {
                return this.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DEL))
                        && this.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_DEL));
            }
            return super.deleteSurroundingText(beforeLength,afterLength);
        }

        @Override
        public boolean sendKeyEvent(KeyEvent event) {
            if (event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
                return con.sendKeyEvent(event);
            } else {
                return super.sendKeyEvent(event);
            }
        }

        @Override
        public boolean commitText(CharSequence text,int newCursorPosition) {
            return con.commitText(text,newCursorPosition);
        }
    };
    try {
        return public_con;
    } catch (Exception e) {
        return super.onCreateInputConnection(outAttrs);
    }
}

主要问题是当我聚焦视图时 WebView 崩溃

2021-01-15 12:50:50.558 14424-14424/com.xxx.xxx.xxx E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.xxx.xxx.xxx,PID: 14424
    java.lang.NullPointerException: Attempt to invoke interface method 'android.os.Handler android.view.inputmethod.InputConnection.getHandler()' on a null object reference
        at android.view.inputmethod.InputConnectionWrapper.getHandler(InputConnectionWrapper.java:286)
        at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:1964)
        at android.view.inputmethod.InputMethodManager$DelegateImpl.startInput(InputMethodManager.java:585)
        at android.view.ImeFocusController.checkFocus(ImeFocusController.java:161)
        at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:5091)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7660)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

当它没有崩溃时,在像素3a上,图片可以在一个块中删除但不能在视频中删除,看起来退格键正在删除不可见的字符 但以我的荣誉为例,我什至不能使用字符键盘,我只能用键盘写数字,而在三星上我根本不能使用键盘

我可以删除所有设备上的图片,但不能删除视频,并且取决于我是否可以使用键盘

这就是我在 contenteditable div 中添加 html5 视频的方式

RE.insertVideo = function(url,alt) {
    var vid = document.createElement('video');
    vid.setAttribute("src",url)
    vid.setAttribute("alt",alt)
    vid.setAttribute('playsinline',true)
    vid.setAttribute('webkit-playsinline',true)
    vid.setAttribute('autoplay',true)
    vid.setAttribute('loop',true)
    vid.setAttribute('muted',true)
    vid.setAttribute('width',RE.editor.clientWidth - 20)
    vid.onload = RE.updateHeight;
    RE.insertHTML(vid.outerHTML);
    RE.callback("input");
}

然后我正在评估 Java 中的 JS

public void insertVideo(String url,int width) {
    exec("javascript:RE.prepareInsert();");
    exec("javascript:RE.insertVideoW('" + url + "','" + width + "');");
}

感谢您的时间:)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)