问题描述
在此great关于在 WebViewClient
和 WebChromeClient
看来,如果你使用一个,你不应该的差异解释是使用其他(为同一web视图对象)。
From this great explanation about the differences between WebViewClient
and WebChromeClient
it seems that if you use one, you shouldn't be using the other (for the same WebView object).
我的理解是否正确?
如果没有,何时总会有同时使用 WebViewClient
的和的 WebChromeClient
为同一的WebView
对象?
If not, when would one use both WebViewClient
and WebChromeClient
for the same WebView
object?
有没有的情况下只使用一个例子中,两个 WebViewClient
的和的 WebChromeClient
对于相同的的WebView
对象将完成一定的目标?
Is there an example of a situation where only use both WebViewClient
and WebChromeClient
for the same WebView
object would accomplish a certain goal?
推荐答案
您当然可以同时使用,他们只是有不同的功能。设置自己的自定义WebViewClient,您可以处理onPageFinished,shouldOverrideUrlLoading等,WebChromeClient,您可以处理JavaScript的警报()等功能。
You certainly can use both, they just have different functions. Setting your own custom WebViewClient lets you handle onPageFinished, shouldOverrideUrlLoading, etc., WebChromeClient lets you handle Javascript's alert() and other functions.
只是让你自己的类,例如:
Just make your own class, for example:
public class MyWebChromeClient extends WebChromeClient {
//Handle javascript alerts:
@Override
public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result)
{
Log.d("alert", message);
Toast.makeText(context, message, 3000).show();
result.confirm();
return true;
};
...
和/或
public class MyWebViewClient extends WebViewClient {
@Override
//Run script on every page, similar to Greasemonkey:
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:alert('hi')");
}
...
刚刚改写文档中描述的功能,然后设置你的客户的onCreate有:
Just override the functions described in the documentation, then set your client in onCreate with:
webview.setWebViewClient(new MyWebViewClient());
webview.setWebChromeClient(new MyWebChromeClient());
这篇关于是否WebViewClient和WebChromeClient相互排斥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!