单击 URL 打开默认浏览器 [英] Clicking URLs opens default browser
问题描述
我在我的 WebView
中加载了一个外部 URL.现在我需要的是,当用户单击加载的页面上的链接时,它必须像普通浏览器一样工作并在同一个 WebView
中打开链接.但是它正在打开默认浏览器并在那里加载页面?
I have loaded an external URL in my WebView
. Now what I need is that when the user clicks on the links on the page loaded, it has to work like a normal browser and open the link in the same WebView
. But it's opening the default browser and loading the page there?
我已启用 JavaScript.但它仍然不起作用.我是不是忘记了什么?
I have enabled JavaScript. But still it's not working. Have I forgotten something?
推荐答案
如果您使用的是 WebView
如果您不想要默认的 Android 行为,则必须自己拦截点击.
If you're using a WebView
you'll have to intercept the clicks yourself if you don't want the default Android behaviour.
您可以使用 监视 WebView
中的事件WebViewClient
.你想要的方法是 shouldOverrideUrlLoading()
.这允许您在选择特定 URL 时执行自己的操作.
You can monitor events in a WebView
using a WebViewClient
. The method you want is shouldOverrideUrlLoading()
. This allows you to perform your own action when a particular URL is selected.
You set the WebViewClient
of your WebView
using the setWebViewClient()
method.
如果您查看 SDK 中的 WebView
示例 有一个例子可以满足您的需求.就这么简单:
If you look at the WebView
sample in the SDK there's an example which does just what you want. It's as simple as:
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
这篇关于单击 URL 打开默认浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!