我正在android中构建一个富文本编辑器。为此,我使用webView
contentEditable div.
要添加样式,我调用JavaScript
。这一切都很好,除非我调用JavaScript插入图像或水平规则。当我使用JavaScript插入这些内容时,如果我尝试按后退按钮删除图像或水平规则,则它不起作用。
奇怪的是,如果我先输入任何其他字符,然后插入图像或水平线,我可以删除图像/水平线,但不能删除我在图像/水平线规则之前输入的字符。< / p>
我尝试在每个状态下打印HTML,检查选择/范围等,似乎找不到任何可能解释为什么我无法删除图像的状态不同的东西等。
答案 0 :(得分:1)
Android: Backspace in WebView/BaseInputConnection
子类Webview并覆盖此人的问题所示的方法。
在某些手机上,只有这个人的问题才能满足要求。链接的答案将完成与其他手机兼容的代码。但是,您继承了InputConnectionWrapper。没有输入连接。然后在自定义webview中返回该包装器。
仅仅是一个FYI,这个链接对情况有更详细的解释,但是我试图快速实现他们的想法并且它没有用。对我来说可能太复杂了。我尝试他们的解决方案而不是我上面提到的原因是因为我上面提到的解决方案导致语音到文本功能无法正常工作。 Android - cannot capture backspace/delete press in soft. keyboard
答案 1 :(得分:0)
我使用WebView
和JavaScript
实现了richTextEditor。
插入/删除我添加的图片时没有问题 内容可编辑的html页面。我用于插入图片的代码是
String exeSucess = "document.execCommand('insertHtml', false,'<img src=\""
+ selectedImagePath + "\" height=auto width=200 ></img>');";
//Then code for executing this javascript.
感谢。
答案 2 :(得分:-1)
<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
密钥
这是delete()
函数;
public void delete(){
loadurl("javascript:document.execCommand('delete', false, null);");
}