How to detect single Tap on WebView

喜夏-厌秋 提交于 2019-12-11 11:05:21

问题


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



来源:https://stackoverflow.com/questions/20022384/how-to-detect-single-tap-on-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!