并且比较强大的是,还可以直接跟js相互调用。
webview有两个方法:setWebChromeClient和setWebClient
setWebChromeClient主要处理解析,渲染网页等浏览器做的事情
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); } } } );
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();
} public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); //设置字符集编码webview1.getSettings().setDefaultTextEncodingName("UTF-8");
webview1.loadUrl("http://www.android100.org/");
联系客服