gpt4 book ai didi

android - 仅设置 webViewClient 会导致在 webview 中打开链接

转载 作者:行者123 更新时间:2023-11-29 00:27:59 25 4
gpt4 key购买 nike

通常,当在 WebView 中单击链接时, Intent 会导致 Web 浏览器或支持该 Intent 的其他 Activity 打开。

WebView webView = new WebView(this);
setContentView(webView);
webView.loadUrl("http://google.com");

但是,当我只设置一个空的 WebViewClient 时......

WebView webView = new WebView(this);
webView.setWebViewClient(new WebViewClient() {});
setContentView(webView);
webView.loadUrl("http://google.com");

…链接在 WebView 中打开并且没有触发任何 Intent !怎么会?

最佳答案

这是设计使然。

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

To open links clicked by the user, simply provide a WebViewClient for your WebView, using setWebViewClient(). For example:

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

That's it. Now all links the user clicks load in your WebView.

http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)

If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url

shouldOverrideUrlLoading 默认返回 false

关于android - 仅设置 webViewClient 会导致在 webview 中打开链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17940882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com