How to detect single Tap on WebView

2019-07-28 04:26发布

问题:

How to detect single Tap on the WebView. I am displaying WebView as a ViewPager page. So, I can't use OnTouchListener. And OnClickListener doesn't work on WebView.

回答1:

If you had a link to get the event you can use something like this with shouldOverrideUrlLoading:

webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                try {
                    String host = null;
                    if (null != url && null != (host = Uri.parse(url).getHost())) {
                        if (host.equals("myclikableUrl")) {
                            clickEvent();
                        } 

                        return true;
                    } else {

                        return false;
                    }
                } catch (Exception e) {
                    return false;
                }
            }.....
});


回答2:

Try to crete your own class that extends WebView and here override dispatchTouchEvent(MotionEvent ev). Also you could add GestureDetector and catch single tap on webview http://developer.android.com/training/gestures/detector.html