简体   繁体   English

如何确定HTML元素何时在Android WebView中获得焦点?

[英]How to determine when an HTML element gains focus in Android WebView?

I am trying to determine when a Text Field in a html page is focused from the Android WebView. 我试图确定何时从Android WebView聚焦html页面中的文本字段。 Some notes: I do not own the web page so I can't add anything directly to the webpage. 一些注意事项:我没有网页,所以我无法直接在网页上添加任何内容。

What I have tried: 我尝试过的:

    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);
    webView.addJavascriptInterface(new Object() {

        @JavascriptInterface
        public void displayScanButton() {
            WebViewActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.d("SCAN", "DISPLAY SCAN BUTTON");
                    toolbar.setVisibility(View.VISIBLE);
                }
            });
        }

        @JavascriptInterface
        public void hideScanButton() {
            WebViewActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.d("SCAN", "HIDE SCAN BUTTON");
                    toolbar.setVisibility(View.GONE);
                }
            });
        }
    }, "Android");

@Override
public void onPageFinished(WebView view, String url) {
    view.loadUrl("javascript:document.getElementById('tNewLabels').onfocus = Android.displayScanButton; document.getElementById('tNewLabels').onfocusout = Android.hideScanButton;");
}

I have a couple variations of that as well but that is what I currently have. 我也有几种不同的变化,但这就是我现在拥有的。 It does not set onfocus for the input field. 它不为输入字段设置onfocus。

Basically what I want is when the user clicks on the input field to type in text, I display my toolbar that I have attached to the keyboard for this specific element only. 基本上我想要的是当用户点击输入字段以输入文本时,我只显示我已连接到键盘的工具栏。 And when the user no longer has focus of the input field, hide the toolbar. 当用户不再具有输入字段的焦点时,隐藏工具栏。 I would think there would be a way since the WebView knows when to open up the keyboard and when to close it. 我认为会有一种方法,因为WebView知道何时打开键盘以及何时关闭它。 Possibly a way to intercept this? 可能是拦截这个的方法吗?

Thanks for any help. 谢谢你的帮助。

Try this: 尝试这个:

document.getElementById('tNewLabels').onfocus = function() { Android.displayScanButton(); };
document.getElementById('tNewLabels').onfocusout = function() { Android.hideScanButton(); };

For the Android-Java bridge to work, your function must have the interface object as the "this" variable. 要使Android-Java桥能够工作,您的函数必须将接口对象作为“this”变量。 Assigning the function directly to the event strips it from its attachment to the interface object. 将函数直接分配给事件会将其从对接口对象的附件中剥离。 I heard it used to work anyway in older versions of the WebView, but it no longer does. 我听说它曾经在旧版本的WebView中工作,但它不再适用。

And sorry for reviving an old question, you probably have already figured it out. 抱歉恢复旧问题,你可能已经弄明白了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM