点击网址打开默认浏览器 [英] Clicking URLs opens default browser
问题描述
我有一个简单的问题,我在的WebView
加载外部URL。现在,我需要的是,当用户点击上加载的页面上的链接,它的工作像一个正常的浏览器,并打开相同的的WebView
的链接。但它打开默认的浏览器并没有加载页面?
I have a simple problem, 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
href="http://developer.android.com/reference/android/webkit/WebViewClient.html"><$c$c>WebViewClient$c$c>.你想要的方法是<一个href="http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading%28android.webkit.WebView,%20java.lang.String%29"><$c$c>shouldOverrideUrlLoading()$c$c>.这可以让你在选择一个特定的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.
您设置 WebViewClient
你的的WebView
使用<一个href="http://developer.android.com/reference/android/webkit/WebView.html#setWebViewClient%28android.webkit.WebViewClient%29"><$c$c>setWebViewClient()$c$c>方法。
You set the WebViewClient
of your WebView
using the setWebViewClient()
method.
如果你看一下<一href="http://developer.android.com/intl/de/resources/tutorials/views/hello-webview.html"><$c$c>WebView$c$c>在SDK示例的有哪些呢,你想要的东西的一个例子。它就是这么简单:
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;
}
}
这篇关于点击网址打开默认浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!