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

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

    You need to add an extra line of code, like so-

    webview.loadUrl(url);
    webview.setWebViewClient(new MyWebViewClient());
    

    And add an additional class MyWebViewClient like this-

    /* Class for webview client */
    class MyWebViewClient extends WebViewClient {
    
    // show the web page in webview but not in web browser
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    
        view.loadUrl(url);
    
        return true;
    }
    
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
    }
    
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }
    
    @Override
    public void onLoadResource(WebView view, String url) {
        super.onLoadResource(view, url);
    
    }
    
    }
    

提交回复
热议问题