【问题标题】:Can't delete image in contenteditable div on Android无法在 Android 上的 contenteditable div 中删除图像
【发布时间】:2013-08-07 01:13:25
【问题描述】:

我正在构建一个在 android 中的富文本编辑器。为此,我使用webViewcontentEditable div.

要添加样式,我调用JavaScript。这一切都很好,除非我调用 JavaScript 来插入图像或水平线。当我使用 JavaScript 插入这些东西时,如果我尝试按后退按钮删除图像或水平线,它不起作用。

奇怪的是,如果我先输入任何其他字符,然后插入图像或横线,我可以删除图像/横线就好了,但不能删除我在图像/横线之前输入的字符。

我已经尝试在每个状态下打印出 HTML,检查选择/范围等,但似乎找不到任何与状态不同的地方,这可以解释为什么我不能删除图像等。

【问题讨论】:

  • 您能否向我们提供一些关于您如何编写 HTML+Javascript 和 Android Java 代码(我猜是 WebChromeClient)的意见?
  • 您没有提供足够精确的信息,我敢猜测,由于您在幕后有 HTML,因此您的退格实际上会删除标签的最后一部分(这足以停止它显示)以及您无法删除项目之前视觉上的内容的原因是进一步按下退格键会删除更多标签。 E..
    显示,然后退格,你有
  • 显示一些 JS 代码,显示一些示例 div 内容 (stackoverflow.com/questions/how-to-ask) 你应该实现并向 div 添加一个事件监听器,它知道如何删除一个完整的 HTML 元素,例如按 Backspace 或 Delete 键时。

标签: android webview contenteditable richtext


【解决方案1】:

Android: Backspace in WebView/BaseInputConnection

子类 Webview 并覆盖这个人的问题所示的方法。

在某些手机上,只有男生的问题才能满足要求。该链接的答案将完成与其他手机兼容的代码。但是,您将 InputConnectionWrapper 子类化。不是输入连接。然后在您的自定义 webview 中返回该包装器。

仅供参考,此链接对情况有更详细的解释,但是我尝试快速实施他们的想法,但没有奏效。也许对我的目的来说太复杂了。我之所以尝试他们的解决方案而不是我上面提到的解决方案是因为我上面提到的解决方案导致语音转文本功能无法正常工作。 Android - cannot capture backspace/delete press in soft. keyboard

【讨论】:

    【解决方案2】:

    我已经使用WebViewJavaScript 实现了一个richTextEditor。

    我在插入/删除已添加的图像时没有问题 内容可编辑的 html 页面。我用于插入图像的代码是

    String exeSucess = "document.execCommand('insertHtml', false,'<img src=\""
            + selectedImagePath + "\" height=auto width=200 ></img>');";
       //Then code for executing this javascript.
    

    谢谢。

    【讨论】:

    • 用光标删除图像没有问题吗?我在这样做时遇到了各种各样的问题。我尝试覆盖 deleteSurroundingText 方法,stackoverflow 上的许多其他线程都建议使用该方法,但实际上并没有任何运气。当覆盖它时,它破坏了所有的自动完成状态,因此会频繁插入或删除随机字符——显然是一个大问题。
    • 我可以通过执行上面的 javascript 来插入图像,也可以通过按下 SoftKeyboard 中的 backpress 按钮来删除它。在 nexus,htc one v 上工作,但在非常低端的设备中它有一些卡住的问题。
    【解决方案3】:
    <div contenteditable="true"></div>
    

    我用它在 webview 中做一个富编辑器

    然后在Webview中覆盖方法TapInputConnection

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        return new TapInputConnection(super.onCreateInputConnection(outAttrs));
    }
    
    
    
    class TapInputConnection implements InputConnection {
    
        private InputConnection mConnection;
    
    
        public TapInputConnection(InputConnection conn){
            this.mConnection = conn;
        }
    
        @Override
        public CharSequence getTextBeforeCursor(int n, int flags) {
            return mConnection.getTextBeforeCursor(n, flags);
        }
    
        @Override
        public CharSequence getTextAfterCursor(int n, int flags) {
            return mConnection.getTextAfterCursor(n, flags);
        }
    
        @Override
        public CharSequence getSelectedText(int flags) {
            return mConnection.getSelectedText(flags);
        }
    
        @Override
        public int getCursorCapsMode(int reqModes) {
            return mConnection.getCursorCapsMode(reqModes);
        }
    
        @Override
        public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
            return mConnection.getExtractedText(request, flags);
        }
    
        @Override
        public boolean deleteSurroundingText(int beforeLength, int afterLength) {
            return mConnection.deleteSurroundingText(beforeLength, afterLength);
        }
    
        @Override
        public boolean setComposingText(CharSequence text, int newCursorPosition) {
            return mConnection.setComposingText(text, newCursorPosition);
        }
    
        @Override
        public boolean setComposingRegion(int start, int end) {
            return mConnection.setComposingRegion(start, end);
        }
    
        @Override
        public boolean finishComposingText() {
            return mConnection.finishComposingText();
        }
    
        @Override
        public boolean commitText(CharSequence text, int newCursorPosition) {
            return mConnection.commitText(text, newCursorPosition );
        }
    
        @Override
        public boolean commitCompletion(CompletionInfo text) {
            return mConnection.commitCompletion(text);
        }
    
        @Override
        public boolean commitCorrection(CorrectionInfo correctionInfo) {
            return mConnection.commitCorrection(correctionInfo);
        }
    
        @Override
        public boolean setSelection(int start, int end) {
            return mConnection.setSelection(start, end);
        }
    
        @Override
        public boolean performEditorAction(int editorAction) {
            return mConnection.performEditorAction(editorAction);
        }
    
        @Override
        public boolean performContextMenuAction(int id) {
            return mConnection.performContextMenuAction(id);
        }
    
        @Override
        public boolean beginBatchEdit() {
            return mConnection.beginBatchEdit();
        }
    
        @Override
        public boolean endBatchEdit() {
            return mConnection.endBatchEdit();
        }
    
        @Override
        public boolean sendKeyEvent(KeyEvent event) {
            if (event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
                if (event.getAction() == KeyEvent.ACTION_UP) {
                    delete();
                }
                return true;
            }
            return mConnection.sendKeyEvent(event);
        }
    
        @Override
        public boolean clearMetaKeyStates(int states) {
            return false;
        }
    
        @Override
        public boolean reportFullscreenMode(boolean enabled) {
            return mConnection.reportFullscreenMode(enabled);
        }
    
        @Override
        public boolean performPrivateCommand(String action, Bundle data) {
            return mConnection.performPrivateCommand(action, data);
        }
    
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean requestCursorUpdates(int cursorUpdateMode) {
            return mConnection.requestCursorUpdates(cursorUpdateMode);
        }
    }
    

    你已经意识到我覆盖了sendKeyEvent,我自己处理Delete Key

    这是delete()函数;

    public void delete(){
      loadurl("javascript:document.execCommand('delete', false, null);");
    }
    

    【讨论】:

    • 请,请,请更改您的格式。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    相关资源
    最近更新 更多