首页 文章

如何避免在同一webview中打开webview链接

提问于
浏览
0

当用户点击webview中的链接时,我想显示一个对话框,询问用户是否想在默认浏览器中查看它,如果他选择YES,那么他应该被带到默认浏览器,否则它不应该加载链接
但是,我面临的问题是,我能够在WebViewClient的重写方法shouldOverrideUrlLoading()的run()中显示对话框 . 当用户选择YES时,我正在执行startActivity(新Intent(Intent.ACTION_VIEW,Uri.parse(url)));以在默认浏览器中显示它 . 但是,无论用户选择是/否,它都会在webview中显示我想要避免的链接 . 任何建议表示赞赏...... TIA

2 回答

  • 1
    WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.setWebViewClient(new WebViewClient());
    
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("www.example.com")) {
                // This is my web site, so do not override; let my WebView load the page
                return false;
            }
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }
    

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

  • 0

    我通过在shouldOverrideUrlLoading()方法中重新加载相同的URL解决了这个问题 . 谢谢你的建议

相关问题