Android:WebView/BaseInputConnection中的Backspace

And*_*kin 15 android android-input-method android-4.2-jelly-bean

Android(4.2)中的软键盘退格有问题.

我在WebView(CodeMirror)中有一个自定义编辑器,它在<textarea>内部使用空.似乎退格不是由Android系统发送的,除非它认为有一些文本<textarea>.

WebView onCreateInputConnection试图勉强降低软输入:

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    Log.d("CustomWebView", "onCreateInputConnection(...)");
    BaseInputConnection connection = new BaseInputConnection(this, false);
    outAttrs.inputType = InputType.TYPE_NULL;
    outAttrs.imeOptions = EditorInfo.IME_ACTION_NONE;
    outAttrs.initialSelStart = -1;
    outAttrs.initialSelEnd = -1;

    return connection;
}
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用,甚至onKeyUp不需要退格.

如何强制软键盘始终发送退格?

And*_*kin 33

好的,终于搞清楚了.

在Android 4.2中(也许在早期版本中),退格不是sendKeyEvent(..., KeyEvent.KEYCODE_DEL)由标准软键盘发送的.相反,它被发送为deleteSurroundingText(1, 0).

因此,我的解决方案是InputConnection使用以下方法进行自定义:

@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);
}
Run Code Online (Sandbox Code Playgroud)

注意:如果我在这里做了一些蠢事,请告诉我,因为这是我为Android写的第3天.