setWebViewClient
Android のとの違いは何ですかsetWebChromeClient
?
ベストアンサー1
からソースコード:
// 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 に alert() サポートを追加する
一見すると、違いが多すぎるWebViewクライアント&WebChromeクライアントただし、基本的には、HTML をレンダリングする以外に多くの機能を必要としない WebView を開発している場合は、 を使用できますWebViewClient
。一方、レンダリングするページの favicon を読み込むなどしたい場合は、 オブジェクトを使用してWebChromeClient
をオーバーライドする必要がありますonReceivedIcon(WebView view, Bitmap icon)
。
ほとんどの場合、これらのことを心配したくない場合は、次のようにすればよいだけです。
webView= (WebView) findViewById(R.id.webview);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
そして、WebView には (理論上は) すべての機能が (Android ネイティブ ブラウザーとして) 実装されます。