您的位置:首页 > 移动开发

SetWebViewClient和 SetWebChromeClient的区别

2016-08-10 21:26 381 查看
参考;http://blog.csdn.net/dufangyu1990/article/details/39693181

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

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 aWebViewClient. 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).

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).

说的很清楚了setWebChromeClient比setWebViewClient功能强大一些,

setWebClient帮助WebView处理各种通知、请求事件

onLoadResource

onPageStart

onPageFinish

onReceiveError

onReceivedHttpAuthRequest

如果你不需要太多的功能而仅仅是渲染一个HTML网页,只需要用setWebViewClient就可以了,但是如果要处理比较复杂的事务,就考虑用后者

2.setWebChromeClient辅助WebView处理JavaScript的对话框,网站图标,网站title,加载进度等

onCloseWindow(关闭WebView)

onCreateWindow()

onJsAlert (WebView上alert是弹不出来东西的,需要定制你的WebChromeClient处理弹出)

onJsPrompt

onJsConfirm

onProgressChanged

onReceivedIcon

onReceivedTitle

例如添加进度条:

webview1.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view, int progress)
{
setProgress(progress * 100);
if(progress == 100){
imageView1.setVisibility(View.GONE);
tv1.setVisibility(View.GONE);
pb1.setVisibility(View.GONE);
fy1.setVisibility(View.GONE);
}
}
}
);


另外如果你怕顾虑太多,可以这样使用

webView= (WebView) findViewById(R.id.webview);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);


但是我感觉这样子好奇怪
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript