您的位置:首页 > 移动开发

Android:让Link始终保持在程序的WebView中跳转

2014-05-22 09:49 295 查看
在Android的WebView中,当点击调用网页的链接时,默认的动作是跳转到系统设定的默认浏览器中。如果想让链接始终在当前WebView中跳转的话,就需要添加以下代码:

WebView webView = (WebView) findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient());


如果只是想让特定的URL保持在WebView中跳转的话,可以通过重写WebViewClient来实现,示例如下:

private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("192.168.3.95")) {
// 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;
}
}


其中的192.168.3.95可以转换成任何想要保持在WebViewClient中跳转的Host名称,例如www.example.com。

最后别忘了把webView.setWebViewClient(new WebViewClient());改为webView.setWebViewClient(new MyWebViewClient());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: