WebView加载一个web页面:
private WebView wv;
.....
wv=(WebView) findViewById(R.id.wV);
wv.loadUrl("http://www.baidu.com");
这样会调用本机的浏览器,在浏览器中打开web页面。只让本应用程序的webview加载网页而不调用外部浏览器的办法如下:设置WebViewClient,并重写WebViewClient的shouldOverrideUrlLoading方法返回true
mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
});
原因: WebViewClient的shouldOverrideUrlLoading方法的默认实现是直接返回false的:
/** 。。。。。。。
* @param view The WebView that is initiating the callback.
* @param url The url to be loaded.
* @return True if the host application wants to leave the current WebView
* and handle the url itself, otherwise return false.
*/
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
WebView的loadData用法:
WebView一般为了节省资源使用的是UTF-8编码,所以我们在loadData的时候要告诉方法怎样转码。即要告诉它要将unicode编码的内容转成UTF-8编码的内容。有些朋友虽然在loadData的时候设置了编码方式,但是还是显示乱码,这是因为还需要为WebView的text编码指定编码方式。举例如下:
WebView wv = (WebView)findViewById(R.id.webview) ;
String content = getUnicodeContent() ;
wv.getSettings().setDefaultTextEncodingName("UTF-8") ;
wv.loadData(content, "text/html", "UTF-8") ;
参考:http://blog.csdn.net/u011494050/article/details/41512777?utm_source=tuicool&utm_medium=referral