Clicking URLs opens default browser

前端 未结 6 513
独厮守ぢ
独厮守ぢ 2020-11-22 13:09

I have loaded an external URL in my WebView. Now what I need is that when the user clicks on the links on the page loaded, it has to work like a normal browser

6条回答
  •  庸人自扰
    2020-11-22 13:29

    If you're using a WebView you'll have to intercept the clicks yourself if you don't want the default Android behaviour.

    You can monitor events in a WebView using a WebViewClient. The method you want is shouldOverrideUrlLoading(). This allows you to perform your own action when a particular URL is selected.

    You set the WebViewClient of your WebView using the setWebViewClient() method.

    If you look at the WebView sample in the SDK there's an example which does just what you want. It's as simple as:

    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
    

提交回复
热议问题