webview中setWebChromeClient和setWebClient区别

webview中setWebChromeClient和setWebClient区别

在用webview加载html页面时候,有时候会用到setWebClient和setWebChromeClient,他们之间的不同在于:

1.setWebClient就是帮助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();

            }

                   public boolean shouldOverrideUrlLoading(WebView view, String url) {

                                view.loadUrl(url);

                                  returntrue;

         }

});

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);
                }
            }
        }
        );    

你可能感兴趣的:(android)