从
source code:
// Instance of WebViewClient that is the client callback.
private volatile WebViewClient mWebViewClient;
// Instance of WebChromeClient for handling all chrome functions.
private volatile WebChromeClient mWebChromeClient;
// SOME OTHER SUTFFF.......
/**
* Set the WebViewClient.
* @param client An implementation of WebViewClient.
*/
public void setWebViewClient(WebViewClient client) {
mWebViewClient = client;
}
/**
* Set the WebChromeClient.
* @param client An implementation of WebChromeClient.
*/
public void setWebChromeClient(WebChromeClient client) {
mWebChromeClient = client;
}
使用WebChromeClient可以处理Javascript对话框,图标,标题和进度。看看这个例子:Adding alert() support to a WebView
乍一看,有太多差别WebViewClient& WebChromeClient.但是,基本上:如果你正在开发一个WebView,不需要太多的功能,但渲染HTML,你可以只使用一个WebViewClient。另一方面,如果你想(例如)加载你正在渲染的页面的图标,你应该使用WebChromeClient对象,并重写onReceivedIcon(WebView视图,位图图标)。
大多数时候,如果你不想担心这些事情…你可以这样做:
webView= (WebView) findViewById(R.id.webview);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
和你的WebView(在理论上)将实现所有的功能(如android本机浏览器)。