我在 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;
}
然而,这并不起作用,甚至onKeyUp
不要求退格键。
如何强制软键盘始终发送退格键?
好吧,终于弄清楚了。
在 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);
}
注意:如果我在这里做了一些愚蠢的事情,请告诉我,因为这是我为 Android 编写的第三天。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)