Android webview调用loadurl时启动浏览器
我创建了一个 Activity
,它在 LinearLayout
中具有标题和 Web 视图。在 onResume()
方法中,它调用 webView.loadUrl(url)
。问题在于,该 Activity 首先显示标题,而屏幕的其余部分为空白,然后启动设备浏览器并显示 URL 页面。我想看到的是标题下方的 WebView
中显示的页面。可能是什么问题?
编辑: 好的,做了一些进一步的搜索,发现了这个:
它指向 此处的 WebView
教程。
只需实现 Web 客户端并进行设置即可。
I created an Activity
that has a title and a web view in a LinearLayout
. In the onResume()
method it calls webView.loadUrl(url)
. The problem is that the activity first shows the title with the rest of the screen blank, then the device browser is launched with the page for the URL. What I want to see is the page being shown in the WebView
below the title. What could be the problem?
Edit:
Ok, did some further search and found this one:
Clicking URLs opens default browser
It points to the WebView
tutorial here.
Just implement the web client and set it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
让你的活动像这样。
Make your Activity like this.
我遇到了同样的问题,我找到了解决方案
Android 关于 WebView 的官方文档
这是我的
onCreateView()
方法,这里我使用了两种方法来打开网址方法1是在浏览器中打开url并
方法2是在您想要的WebView中打开url。
我的应用程序使用方法 2,这是我的代码:
I was facing the same problem and I found the solution
Android's official Documentation about WebView
Here is my
onCreateView()
method and here i used two methods to open the urlsMethod 1 is opening url in Browser and
Method 2 is opening url in your desired WebView.
And I am using Method 2 for my Application and this is my code:
如果您看到空页面,请启用 JavaScript。
If you see an empty page, enable JavaScript.
试试这个代码...
Try this code...
简单回答你可以这样使用
Simply Answer you can use like this
如果您使用 webChromeClient,我建议您同时使用 webChromeClient 和 webViewClient。因为webChromeClient不提供shouldOverrideUrlLoading。两者都用就可以了。
If you're using webChromeClient I'll suggest you to use webChromeClient and webViewClient together. because webChromeClient does not provides shouldOverrideUrlLoading. It is okay to use both.
我刚刚发现这取决于 URL 的格式:
我的代码只是使用
不需要
设置至少就我而言。
也许这对你们中的一些人有用。
I just found out that it depends on the formatting of the URL:
My code just uses
no need to set
at least in my case.
Maybe that's useful for some of you.
我的问题最终是我需要先清除历史记录,然后才能在不打开外部浏览器的情况下在站点之间切换。
My problem ended up being that I needed to do a clearHistory before I could switch between sites without opening an external browser.
根据 Maudicus 和 Hit 的建议回答我的问题。
请在此处查看 WebView 教程。
只需实现 Web 客户端并将其设置在 loadUrl 之前即可。最简单的方法是:
要对 Web 内容进行更高级的处理,请考虑 ChromeClient。
Answering my question based on the suggestions from Maudicus and Hit.
Check the WebView tutorial here.
Just implement the web client and set it before loadUrl. The simplest way is:
For more advanced processing for the web content, consider the ChromeClient.
使用这个:
Use this:
像这样使用:
use like this: