我遇到WebView
的问题,我使用带有WebView
的zsseditor来显示,从键盘输入文字,插入图片或视频。然后,我无法删除段落中的最新字母(几乎我无法删除图像html标记)。我通过CustomWebview解决了这个问题,删除了图像html标签。
public class CustomWebView extends WebView {
/**
* Constructs a new WebView with a Context object.
*
* @param context a Context object used to access application assets
*/
public CustomWebView(Context context) {
super(context);
}
/**
* Constructs a new WebView with layout parameters.
*
* @param context a Context object used to access application assets
* @param attrs an AttributeSet passed to our parent
*/
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* Constructs a new WebView with layout parameters and a default style.
*
* @param context a Context object used to access application assets
* @param attrs an AttributeSet passed to our parent
* @param defStyle the default style resource ID
*/
public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
// http://stackoverflow.com/questions/14560344/android-backspace-in-webview-baseinputconnection
// http://stackoverflow.com/questions/164991Image78/cant-get-backspace-to-work-in-codemirror-under-phonegap-on-android-4-x
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
return new MyCustomInputConnection(this, false);
}
public class MyCustomInputConnection extends BaseInputConnection {
public MyCustomInputConnection(View targetView, boolean fullEditor) {
super(targetView, fullEditor);
}
@Override
public boolean deleteSurroundingText(int beforeLength, int afterLength) {
// magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace
if (beforeLength == 1 && afterLength == 0) {
// backspace
return super.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
&& super.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
}
return super.deleteSurroundingText(beforeLength, afterLength);
}
}
}
我将输入键盘换成日语,越南语,泰语或所有语言而非Alphabet。但是当我点击键盘上的按钮字母时,它不会显示在WebView
上。那么如何修复可以用各种语言显示字母的WebView
?
答案 0 :(得分:1)
我有错误同样的问题并使用此代码解决 你只需在webview中覆盖
```
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
outAttrs.actionLabel = null;
outAttrs.inputType = InputType.TYPE_NULL;
final InputConnection con = new BaseInputConnection(this,false);
public_con = new InputConnectionWrapper(
super.onCreateInputConnection(outAttrs), 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);
}
}
};
try {
return public_con ;
}catch (Exception e){
return super.onCreateInputConnection(outAttrs) ;
}
}
```