SetWebViewClient和 SetWebChromeClient的区别

参考;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功能强大一些,

  1. 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); 
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值