Disable selectable links within WebView

2019-04-17 22:00发布

问题:

I have a webView that is contained within a scrollView. Everything is then contained within a viewPager. When I fling to the next view page, the links that are in the middle of the page are being focused (highlighted with orange around the text). This causes the page to jump down to the nearest link.

Is there a way to disable links from being focusable on touch? I've tried all the settings for the webView such as focusable = false, clickable = false, focusable in touch mode = false, and nothing seems to be working.

回答1:

I managed to do it programmatically like so:

WebView myWebView = (WebView) storyItem.findViewById(R.id.myWebView);
myWebView.setFocusableInTouchMode(false);
myWebView.setFocusable(false);

This makes the links non-focusable, which solved my problem!



回答2:

    webView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });


回答3:

Webview.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {                  
                        return true;
            }
        });

This prevents the webview from opening any links and will also allow you to navigate the page without problems