无法在安卓4.1.1接收键code_DEL上的WebView [英] Cannot receive KEYCODE_DEL on WebView in Android 4.1.1

查看:287
本文介绍了无法在安卓4.1.1接收键code_DEL上的WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要让的WebView 编辑通过创建一个复杂的JavaScript来处理键盘事件。一切工作正常在所有Android版本除了 4.1 。 在 4.1 我可以处理所有的关键除了 KeyEvent.KEY code_DEL 事件。看来,我们不能处理键code_DEL 事件的WebView 中的Andr​​oid 4.1?我很AP preciate如果有人能帮助我在这个问题上

I want to make WebView editable by creating a complex javascript to handle keyboard event. Everything works fine on all Android version except 4.1. In 4.1 I can handle all key events except KeyEvent.KEYCODE_DEL. It seems that we cannot handle KEYCODE_DEL event for WebView in Android 4.1? I am very appreciate if someone can help me on this issue

感谢

推荐答案

看起来像一个Android的bug。为我工作的一个简单的解决方法是设置

Looks like an Android bug. A simple workaround that worked for me was to set

android:targetSdkVersion="15"

在你的Andr​​oidManifest.xml

in your AndroidManifest.xml

在一些调查研究,我现在认为,这是不是一个错误,而是一种蓄意的变化。该的KeyEvent 文件说:

After some more research, I now think that this is not a bug but rather a deliberate change. The KeyEvent documentation says:

由于软输入法可以使用​​输入的多重和创造性的方式   文字,也不能保证一个软键盘上的任意键preSS将   产生的键事件:这是留给IME的决定,并在   事实上发送此类事件被劝阻。你永远不应该依赖   接收的KeyEvent对软输入法的任意键。尤其是,   默认的软件键盘将永远不会发送任何关键事件的任何   应用打靶果冻豆或更高版本,并只发送事件   对于一些$ P $删除和返回键psses应用程序   打靶冰淇淋三明治或更早版本。

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.

在现实中,虽然,它仍然发出了最关键的presses除了Delete键事件。因为我真正需要的所有重要事件,我想出了这个解决方案:

In reality though, it still sends events for most key presses except for the Delete key. Since I really needed all key events I came up with this solution:

首先,创建自己的查看(在我的情况下,它是从的TextView 来源)是这样的:

First, create your own View (in my case it was derived from TextView) like this:

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;
    }
}

二,创建 MyInputConnection 通过继承 BaseInputConnection 是这样的:

Second, create MyInputConnection by subclassing BaseInputConnection like this:

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()方法获取有关外文字母等关键事件。

In your InputConnection class you've got good control on what's going on. For example you can override the commitText() method to get events about keys of foreign language letters etc.

这篇关于无法在安卓4.1.1接收键code_DEL上的WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆