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

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

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

 1 private class MyWebViewClient extends WebViewClient {
 2     @Override
 3     public boolean shouldOverrideUrlLoading(WebView view, String url) {
 4         if (Uri.parse(url).getHost().equals("192.168.3.95")) {
 5             // This is my web site, so do not override; let my WebView load the page
 6             return false;
 7         }
 8         // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
 9         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
10         startActivity(intent);
11         return true;
12     }
13 }

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

最后别忘了把webView.setWebViewClient(new WebViewClient());改为webView.setWebViewClient(new MyWebViewClient());

相关文章:

  • 2021-12-19
  • 2021-08-13
  • 2021-08-31
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-02
  • 2022-12-23
  • 2021-10-16
  • 2022-12-23
  • 2021-05-21
相关资源
相似解决方案