gpt4 book ai didi

android - 无法在 Android 4.1.1 的 WebView 上接收 KEYCODE_DEL

转载 作者:太空宇宙 更新时间:2023-11-03 13:31:22 26 4
gpt4 key购买 nike

我想通过创建一个复杂的 javascript 来处理键盘事件,使 WebView 可编辑。除 4.1 外,所有 Android 版本都运行良好。在 4.1 中,我可以处理除 KeyEvent.KEYCODE_DEL 之外的所有按键事件。在Android 4.1 中,我们似乎无法处理WebViewKEYCODE_DEL 事件?如果有人可以帮助我解决这个问题,我将不胜感激

谢谢

最佳答案

看起来像一个 Android 错误。一个对我有用的简单解决方法是设置

android:targetSdkVersion="15"

在你的 AndroidManifest.xml 中

编辑:

经过更多研究,我现在认为这不是错误,而是有意更改。 KeyEvent 文档说:

As soft input methods can use multiple and inventive ways of inputting text, there is no guarantee that any key press on a soft keyboard will generate a key event: this is left to the IME's discretion, and in fact sending such events is discouraged. You should never rely on receiving KeyEvents for any key on a soft input method. In particular, the default software keyboard will never send any key event to any application targetting Jelly Bean or later, and will only send events for some presses of the delete and return keys to applications targetting Ice Cream Sandwich or earlier.

但实际上,它仍然会为大多数按键发送事件,除了 Delete 键。因为我确实需要所有关键事件,所以我想到了这个解决方案:

首先,创建您自己的 View(在我的例子中,它派生自 TextView),如下所示:

public class MyTextView extends TextView {

...

@Override
public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
editorInfo.actionLabel = null;
editorInfo.inputType = InputType.TYPE_NULL;
editorInfo.imeOptions = EditorInfo.IME_ACTION_NONE;

return new MyInputConnection(this, false);
}

@Override
public boolean onCheckIsTextEditor() {
return true;
}
}

其次,通过子类化 BaseInputConnection 创建 MyInputConnection,如下所示:

public class MyInputConnection extends BaseInputConnection { 

...


// From Android 4.1 this is called when the DEL key is pressed on the soft keyboard (and
// sendKeyEvent() is not called). We convert this to a "normal" key event.
@Override
public boolean deleteSurroundingText(int beforeLength, int afterLength) {
long eventTime = SystemClock.uptimeMillis();
sendKeyEvent(new KeyEvent(eventTime, eventTime,
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE | KeyEvent.FLAG_EDITOR_ACTION));
sendKeyEvent(new KeyEvent(SystemClock.uptimeMillis(), eventTime,
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE | KeyEvent.FLAG_EDITOR_ACTION));
return true;
}

在您的 InputConnection 类中,您可以很好地控制正在发生的事情。例如,您可以覆盖 commitText() 方法来获取有关外语字母键等的事件。

关于android - 无法在 Android 4.1.1 的 WebView 上接收 KEYCODE_DEL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12730635/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com