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

后端 未结 8 1744
再見小時候
再見小時候 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

    You should save in a class variable your current url and check if it's the same with the loaded one. When the user clicks on a link the shouldOverrideUrlLoading is called and check the website. Something like this:

    private String currentUrl;
    
    public ourWebViewClient(String currentUrl) {
        this.currentUrl = currentUrl;
    }
    
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.equals(currentUrl)) {
            view.loadUrl(url);  
        }
        return true;
    }
    

    Important: don't forget set the WebViewClient to your WebView.

    ourWebViewClient webViewClient = new ourWebViewClient(urlToLoad);
    wv.setWebViewClient(webViewClient);
    

提交回复
热议问题