通过Android Activity中的软件键盘收听Webview关键事件 [英] Listen Webview Key Events from software keyboard in Android Activity

查看:120
本文介绍了通过Android Activity中的软件键盘收听Webview关键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在主机Android应用程序中通过Web视图处理软件键盘事件?

例如,我的应用程序的活动"可以在显示Google网站的网络视图的搜索字段中监听键入的内容吗?

考虑到下面描述的方法,如果我将其覆盖为true,则可以这样做,但是很遗憾,我无法做到这一点.有什么想法吗?

public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event)

Added in API level 1
Give the host application a chance to handle the key event synchronously. e.g. menu shortcut key     events need to be filtered this way. If return true, WebView will not handle the key event. If return false, WebView will always handle the key event, so none of the super in the view chain will see the key event. The default behavior returns false.

Parameters
view    The WebView that is initiating the callback.
event   The key event.
Returns
True if the host application wants to handle the key event itself, otherwise return false

我认为shouldOverrideKeyEvent方法只是告诉系统哪个窗口"应该收到关键事件的通知;这不会将WebView javascript事件传递回活动.

如果需要将信息从WebView传递到Activity,则可以使用JavascriptInterface. 请注意,这只能在您控制的网站上;否则,这将是一个安全漏洞,使您的应用可以访问敏感数据.

首先,创建一个类作为您的界面:

class MyJavaScriptInterface{
    //Methods to call via js in the WebView go here
}

然后初始化WebView上的界面:

mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new MyJavaScriptInterface(), "JSInterface");

现在,您说您想将输入的内容从文本字段传递回活动.为此,请在文本字段模糊时注册一个事件侦听器,当该字段失去焦点时将触发该事件侦听器.

<!-- HTML element to listen for blur -->
<input id="field" type="text" />

<script>
  //Communicate with Javascript Interface
  var jsFieldBlur = function(field){
      //Only call if the interface exists
      if(window.JSInterface){
          window.JSInterface.onFieldBlur(field.id, field.value);
      }
  };

  //Obtain reference to DOM element
  var field = document.getElementById("field");

  //Attach blur listener
  field.addEventListener("blur", function( event ) {
      jsFieldBlur(event.target);   
  }, true);
</script>

最后,我们需要将onFieldBlur方法添加到MyJavascriptInterface类,以便可以通过javascript调用它.用这种方法定义的任何方法都必须在@JavascriptInterface之前,以便WebView可以看到它.

class MyJavaScriptInterface{
    @JavascriptInterface
    public void onFieldBlur(String fieldId, String fieldValue){
        //Do something with value
        Toast.makeText(getContext(), fieldId+"="+fieldValue, Toast.LENGTH_LONG).show();
    }
}

Is it possible to handle software keyboard events from a webview in a host Android application?

For example, can my application's Activity listen the typed content in the search field of a webview displaying Google website?

Considering the method described below this would be possible if I overwrite it returning true, but unfortunatelly I was not able to do it. Any ideas?

public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event)

Added in API level 1
Give the host application a chance to handle the key event synchronously. e.g. menu shortcut key     events need to be filtered this way. If return true, WebView will not handle the key event. If return false, WebView will always handle the key event, so none of the super in the view chain will see the key event. The default behavior returns false.

Parameters
view    The WebView that is initiating the callback.
event   The key event.
Returns
True if the host application wants to handle the key event itself, otherwise return false

解决方案

I think the shouldOverrideKeyEvent method simply tells the system which "window" should be getting notified of key events; this does NOT pass WebView javascript events back to the Activity.

If you need to pass information from a WebView to your Activity, you can use the JavascriptInterface. Note that this is only possible on a website that you control; otherwise it would be a security loophole, providing your app with access to sensitive data.

First, create a class to act as your interface:

class MyJavaScriptInterface{
    //Methods to call via js in the WebView go here
}

Then initialize the interface on your WebView:

mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new MyJavaScriptInterface(), "JSInterface");

Now, you said you wanted to pass the typed content from a text field back to the Activity. For that, register an event listener for the text field on blur, which will fire when the field loses focus.

<!-- HTML element to listen for blur -->
<input id="field" type="text" />

<script>
  //Communicate with Javascript Interface
  var jsFieldBlur = function(field){
      //Only call if the interface exists
      if(window.JSInterface){
          window.JSInterface.onFieldBlur(field.id, field.value);
      }
  };

  //Obtain reference to DOM element
  var field = document.getElementById("field");

  //Attach blur listener
  field.addEventListener("blur", function( event ) {
      jsFieldBlur(event.target);   
  }, true);
</script>

Finally, we need to add the onFieldBlur method to the MyJavascriptInterface class so it can be called via javascript. Any methods defined this way must be preceded by @JavascriptInterface in order to be visible to the WebView.

class MyJavaScriptInterface{
    @JavascriptInterface
    public void onFieldBlur(String fieldId, String fieldValue){
        //Do something with value
        Toast.makeText(getContext(), fieldId+"="+fieldValue, Toast.LENGTH_LONG).show();
    }
}

这篇关于通过Android Activity中的软件键盘收听Webview关键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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