【问题标题】:Listen Webview Key Events from software keyboard in Android Activity在 Android Activity 中从软件键盘监听 Webview 键事件
【发布时间】:2014-10-13 17:59:59
【问题描述】:

是否可以在主机 Android 应用程序中处理来自 web 视图的软件键盘事件?

例如,我的应用程序的 Activity 是否可以在显示 Google 网站的 webview 的搜索字段中侦听键入的内容?

考虑到下面描述的方法,如果我覆盖它返回 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

【问题讨论】:

  • 你只想为谷歌或所有网站这样做
  • @joaonlima :恐怕如果有可能,就好像这种情况,人们可能能够跟踪在 webview 支付页面中输入的 3-D 安全密码(when-if)(多数民众赞成在例如线说)

标签: android webview keyevent


【解决方案1】:

我认为shouldOverrideKeyEvent 方法只是告诉系统哪个“窗口”应该得到关键事件的通知;这确实将 WebView javascript 事件传递回 Activity。

如果您需要将信息从 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");

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

<!-- 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();
    }
}

【讨论】:

    【解决方案2】:
    public class WebViewDemo extends Activity {
    
    WebView myWebView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.loadUrl("file:///android_asset/test.html");
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
    
        myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
    
       // myWebView.setWebViewClient(new WebViewClient());
        myWebView.setWebViewClient(new MyWebViewClient());
    }
    
    public class JavaScriptInterface {
        Context mContext;
    
        /** Instantiate the interface and set the context */
        JavaScriptInterface(Context c) {
            mContext = c;
        }
    
        /** Show a toast from the web page */
        public void showToast(String toast) {
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
            startActivity(new Intent(WebViewDemo.this, WebViewDemo.class));
        }
    }
    
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("www.google.com")) {
                // This is my web site, so do not override; let my WebView load the page
                Toast.makeText(getApplicationContext(), "www.google.com", Toast.LENGTH_SHORT).show();
                return false;
            }
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the BACK key and if there's history
        if ((keyCode ==  KeyEvent.KEYCODE_ENTER)) {
    
           // here you can got key event of enter key ( whether you used as for search of any other option
    
            return true;
        }
        // If it wasn't the BACK key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event);
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-15
      • 2011-07-24
      • 2011-07-25
      • 2014-10-08
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      • 1970-01-01
      相关资源
      最近更新 更多