码迷,mamicode.com
首页 > Web开发 > 详细

WebViewClient与WebChromeClient 区别

时间:2014-09-10 15:56:20      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:android   webview   html   webviewclient   webchromeclient   

android应用开发的时候可能会用到webview这个组件,使用过程中可能会接触到WebViewClient与WebChromeClient,那么这两个类到底有什么不同呢?

WebViewClient主要帮助WebView处理各种通知、请求事件的,比如:

onLoadResource,onPageStart,onPageFinish,onReceiveError,onReceivedHttpAuthRequest 等。

webview1.setWebViewClient(new WebViewClient() { 
	public void onReceivedError(WebView view, int errorCode, String description, 
		String failingUrl){
		//Handle the error
		Toast.makeText(getApplicationContext(), "网络连接失败 ,请连接网络。", Toast.LENGTH_SHORT).show();
							}; 
						} 
		}); 
	webview1.getSettings().setDefaultTextEncodingName("UTF-8"); 
	webview1.loadUrl("");


WebChromeClient主要辅助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只是用来处理一些html的页面内容,只用WebViewClient就行了,如果需要更丰富的处理效果,比如JS、进度条等,就要用到WebChromeClient。

更多的时候,你可以这样:

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

当然,有些更精彩的内容还是需要你自己添加的

WebViewClient与WebChromeClient 区别

标签:android   webview   html   webviewclient   webchromeclient   

原文地址:http://blog.csdn.net/gao_chun/article/details/39180791

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!