How to detect single Tap on WebView

2019-07-28 03:52发布

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.

2条回答
干净又极端
2楼-- · 2019-07-28 04:12

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;
                }
            }.....
});
查看更多
冷血范
3楼-- · 2019-07-28 04:25

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

查看更多
登录 后发表回答