Android setWebViewClient 与 setWebChromeClient 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2835556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between setWebViewClient vs. setWebChromeClient?
提问by Pentium10
What's the difference between setWebViewClient
vs. setWebChromeClient
in Android?
setWebViewClient
与setWebChromeClient
Android 中的有什么区别?
采纳答案by Cristian
From the source code:
从源代码:
// Instance of WebViewClient that is the client callback.
private volatile WebViewClient mWebViewClient;
// Instance of WebChromeClient for handling all chrome functions.
private volatile WebChromeClient mWebChromeClient;
// SOME OTHER SUTFFF.......
/**
* Set the WebViewClient.
* @param client An implementation of WebViewClient.
*/
public void setWebViewClient(WebViewClient client) {
mWebViewClient = client;
}
/**
* Set the WebChromeClient.
* @param client An implementation of WebChromeClient.
*/
public void setWebChromeClient(WebChromeClient client) {
mWebChromeClient = client;
}
Using WebChromeClient allows you to handle Javascript dialogs, favicons, titles, and the progress. Take a look of this example: Adding alert() support to a WebView
使用 WebChromeClient 允许您处理 Javascript 对话框、网站图标、标题和进度。看一下这个例子:Adding alert() support to a WebView
At first glance, there are too many differences WebViewClient& WebChromeClient. But, basically: if you are developing a WebView that won't require too many features but rendering HTML, you can just use a WebViewClient
. On the other hand, if you want to (for instance) load the favicon of the page you are rendering, you should use a WebChromeClient
object and override the onReceivedIcon(WebView view, Bitmap icon)
.
乍一看,WebViewClient和WebChromeClient 的区别太多了。但是,基本上:如果您正在开发不需要太多功能但呈现 HTML 的 WebView,您可以只使用WebViewClient
. 另一方面,如果您想(例如)加载您正在渲染的页面的图标,您应该使用一个WebChromeClient
对象并覆盖onReceivedIcon(WebView view, Bitmap icon)
.
Most of the times, if you don't want to worry about those things... you can just do this:
大多数时候,如果你不想担心这些事情......你可以这样做:
webView= (WebView) findViewById(R.id.webview);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
And your WebView will (in theory) have all features implemented (as the android native browser).
并且您的 WebView 将(理论上)实现所有功能(作为 android 本机浏览器)。
回答by maytham-???????
I feel this question need a bit more details. My answer is inspired from the Android Programming, The Nerd Ranch Guide (2nd edition).
我觉得这个问题需要更多细节。我的回答灵感来自 Android Programming, The Nerd Ranch Guide (2nd edition)。
By default, JavaScript is off in WebView. You do not always need to have it on, but for some apps, might do require it.
默认情况下,JavaScript 在 WebView 中处于关闭状态。您并不总是需要打开它,但对于某些应用程序,可能确实需要它。
Loading the URL has to be done after configuring the WebView, so you do that last. Before that, you turn JavaScript on by calling getSettings()
to get an instance of WebSettings and calling WebSettings.setJavaScriptEnabled(true)
. WebSettings is the first of the three ways you can modify your WebView. It has various properties you can set, like the user agent string and text size.
加载 URL 必须在配置 WebView 之后完成,所以你最后做。在此之前,您通过调用getSettings()
以获取 WebSettings 的实例并调用WebSettings.setJavaScriptEnabled(true)
. WebSettings 是您可以修改 WebView 的三种方式中的第一种。它具有您可以设置的各种属性,例如用户代理字符串和文本大小。
After that, you configure your WebViewClient. WebViewClient is an event interface. By providing your own implementation of WebViewClient, you can respond to rendering events. For example, you could detect when the renderer starts loading an image from a particular URL or decide whether to resubmit a POST request to the server.
之后,您配置您的 WebViewClient。WebViewClient 是一个事件接口。通过提供您自己的 WebViewClient 实现,您可以响应渲染事件。例如,您可以检测渲染器何时开始从特定 URL 加载图像或决定是否向服务器重新提交 POST 请求。
WebViewClienthas many methods you can override, most of which you will not deal with. However, you do need to replace the default WebViewClient's implementation of shouldOverrideUrlLoading(WebView, String)
. This method determines what will happen when a new URL is loaded in the WebView, like by pressing a link. If you return true, you are saying, “Do not handle this URL, I am handling it myself.” If you return false, you are saying, “Go ahead and load this URL, WebView, I'm not doing anything with it.”
WebViewClient有许多您可以覆盖的方法,其中大部分您不会处理。但是,您确实需要替换 WebViewClient 的默认实现shouldOverrideUrlLoading(WebView, String)
。此方法确定在 WebView 中加载新 URL 时会发生什么,例如按下链接。如果你返回 true,你是在说,“不要处理这个 URL,我自己处理它。” 如果你返回 false,你是在说,“继续加载这个 URL,WebView,我不会用它做任何事情。”
The default implementation fires an implicit intent with the URL, just like you did earlier. Now, though, this would be a severe problem. The first thing some Web Applications does is redirect you to the mobile version of the website. With the default WebViewClient, that means that you are immediately sent to the user's default web browser. This is just what you are trying to avoid. The fix is simple – just override the default implementation and return false.
默认实现使用 URL 触发隐式意图,就像您之前所做的那样。不过,现在这将是一个严重的问题。某些 Web 应用程序所做的第一件事就是将您重定向到网站的移动版本。使用默认的 WebViewClient,这意味着您会立即被发送到用户的默认 Web 浏览器。这正是您要避免的。修复很简单——只需覆盖默认实现并返回 false。
Use WebChromeClient to spruce things upSince you are taking the time to create your own WebView, let's spruce it up a bit by adding a progress bar and updating the toolbar's subtitle with the title of the loaded page.
使用 WebChromeClient 美化内容由于您正在花时间创建自己的 WebView,让我们通过添加进度条并使用已加载页面的标题更新工具栏的副标题来美化它。
To hook up the ProgressBar, you will use the second callback on WebView: WebChromeClient
.
要连接 ProgressBar,您将使用 WebView: 上的第二个回调WebChromeClient
。
WebViewClient is an interface for responding to rendering events; WebChromeClient is an event interface for reacting to events that should change elements of chrome around the browser. This includes JavaScript alerts, favicons, and of course updates for loading progress and the title of the current page.
WebViewClient 是响应渲染事件的接口;WebChromeClient 是一个事件接口,用于响应应该更改浏览器周围 chrome 元素的事件。这包括 JavaScript 警报、网站图标,当然还有加载进度和当前页面标题的更新。
Hook it up in onCreateView(…)
. Using WebChromeClient to spruce things up
Progress updates and title updates each have their own callback method,
onProgressChanged(WebView, int)
and onReceivedTitle(WebView, String)
. The progress you receive from onProgressChanged(WebView, int)
is an integer from 0 to 100. If it is 100, you know
that the page is done loading, so you hide the ProgressBar by setting its visibility to View.GONE
.
将其连接到onCreateView(…)
. 使用 WebChromeClient 进行优化 Progress 更新和 title 更新都有自己的回调方法,
onProgressChanged(WebView, int)
并且onReceivedTitle(WebView, String)
. 您收到的进度onProgressChanged(WebView, int)
是一个从 0 到 100 的整数。如果是 100,您就知道页面已完成加载,因此您可以通过将 ProgressBar 的可见性设置为 来隐藏 ProgressBar View.GONE
。
Disclaimer:This information was taken from Android Programming: The Big Nerd Ranch Guide with permission from the authors. For more information on this book or to purchase a copy, please visit bignerdranch.com.
免责声明:此信息取自 Android 编程:经作者许可的大书呆子牧场指南。有关本书的更多信息或购买副本,请访问 bignerdranch.com。