在Android WebView中阅读键盘事件 [英] Read Keyboard events in Android WebView

查看:605
本文介绍了在Android WebView中阅读键盘事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在android网络视图中监听关键事件.例如,当用户填写表格时,我应该收到关键事件.这是我的WebView代码

I'm trying to listen to key events in android web view. Example when user is filling a form, I should receive the key events. This is my WebView code

public class MyWebView extends WebView implements OnKeyListener
{
    .....

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        Log.i("PcWebView", "onKeyDown keyCode=" + keyCode);
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event)
    {
        Log.i("PcWebView", "onKeyUp keyCode=" + keyCode);
        return super.onKeyUp(keyCode, event);
    }

    @Override // Listener is initialized in the constructor
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
        Log.i("PcWebView", "onKey keyCode=" + keyCode);
        return false;
    }

    .....

}

当用户在文本框或文本区域中键入内容时,方法onKeyDownonKeyUponKey均不会被调用.是有可能做到这一点,还是Android限制了这种可能违反隐私的行为?

neither of the methods onKeyDown, onKeyUp and onKey are being called when user types into the textboxes or textareas. Is it possible to achieve this or has Android restricted this b'cos of a possible privacy breach?

推荐答案

注意!此解决方案仅支持英文字母.

在没有访问HTML源代码或JS事件的情况下完成.在Android 5.0.2上同时适用于软键盘和硬键盘.

Accomplished without any access to the HTML source code or JS events. Works for me on Android 5.0.2 for both soft and hardware keyboards.

public class MyWebView extends WebView {


    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        return new BaseInputConnection(this, false); //this is needed for #dispatchKeyEvent() to be notified.
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        boolean dispatchFirst = super.dispatchKeyEvent(event);
        // Listening here for whatever key events you need
        if (event.getAction() == KeyEvent.ACTION_UP)
            switch (event.getKeyCode()) {
                case KeyEvent.KEYCODE_SPACE:
                case KeyEvent.KEYCODE_ENTER:
                    // e.g. get space and enter events here
                    break;
            }
        return dispatchFirst;
    }
}

此处的窍门是用简化的方法覆盖系统提供的InputConnection输入实现.默认情况下,Google员工故意阻止开发人员访问事件.因为键事件输入不再是唯一的输入了.有手势,声音,还有更多.官方建议完全停止依赖传统键事件进行文本输入" .在此处查看更多详细信息: https://code.google.com/p/android/issues/detail?id = 42904#c15

The trick here is overriding the system-provided input implementation of InputConnection with a simplified one. Preventing developers from accessing the events by default was made by Googlers on purpose. Because the key event input isn't the only one anymore. There're gestures, voice and more is coming. Official recommendation is to "stop relying on legacy key events for text entry at all". Check out more details here: https://code.google.com/p/android/issues/detail?id=42904#c15

这篇关于在Android WebView中阅读键盘事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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