Don't navigate to other pages in WebView,disable links and references

后端 未结 8 1740
再見小時候
再見小時候 2020-12-25 12:02

I have a webView in Android, and I open a html webpage in it. But it\'s full of links and images, and when I click one of them, it loads in my webview. I want to disable thi

相关标签:
8条回答
  • 2020-12-25 12:46

    Note that the method signature changed in API 24.

    For APIs earlier than 24 the second parameter is String and that signature is now deprecated.

    For APIs 24 and later, the second parameter is WebResourceRequest.

    If your app supports both pre and post API 24 and you want to disable all links you can use this:

      webView.setWebViewClient(new WebViewClient(){
            @Override //for APIs 24 and later
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){ 
               return true; 
            }
            @Override //for APIs earlier than 24
            public boolean shouldOverrideUrlLoading(WebView view, String url){
                return true;
            }
        });
    
    0 讨论(0)
  • 2020-12-25 12:49

    Even I had been facing the same issue. I solved this issue like below

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

    With lambda expressions

    webView.setOnTouchListener((v, event) -> true);
    
    0 讨论(0)
提交回复
热议问题