Android WebView click open within WebView not a default browser

后端 未结 12 2011
迷失自我
迷失自我 2020-11-27 16:34

I did one sample application using WebView, in that web view the URL comes from web services. It\'s working fine, but if I click any link within that WebView, its automatica

12条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 17:04

    You need to set up a WebViewClient in order to override that behavior (opening links using the web browser). You obviously have your WebView declared, but then set up a WebViewClient like so:

    WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.setWebViewClient(new WebViewClient());
    

    Then you need to define your WebViewClient():

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("www.example.com")) {
                // Designate Urls that you want to load in WebView still.
                return false;
            }
    
            // Otherwise, give the default behavior (open in browser)
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }
    

    Then start your WebViewClient:

    WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.setWebViewClient(new MyWebViewClient());
    

    http://developer.android.com/guide/webapps/webview.html

提交回复
热议问题