什么是setWebViewClient之间的区别与setWebChromeClient?区别、setWebViewClient、setWebChromeClient

2023-09-12 06:51:58 作者:抱走请留言

什么是Android中的 setWebViewClient 的差异与 setWebChromeClient

What's the difference between setWebViewClient vs. setWebChromeClient in Android?

推荐答案

在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对话框,网页图标,标题和进度。就拿这个例子的神色:添加报警()支持到的WebView

乍一看,有太多的差异 WebViewClient 和放大器; WebChromeClient.但是,基本上是:如果你正在开发一个web视图不会需要太多的功能,但呈现HTML,你可以使用 WebViewClient 。在另一方面,如果你想(例如)加载您渲染页面的图标,你应该使用 WebChromeClient 对象和覆盖 onReceivedIcon(web视图来看,位图图标)

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 a WebViewClient. 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); 

和您的WebView会(理论上)已经全部功能实现(如Android原生浏览器)。

And your WebView will (in theory) have all features implemented (as the android native browser).