標籤:
android.webkit.WebView本身就是一個瀏覽器實現,它的核心基於開源WebKit引擎。
常用方法:
void goBack():後退
void goForward():前進
void loadUrl(String url):載入網頁
boolean ZoomIn():放大網頁
boolean zoomOut():縮小網頁
更多的請查閱官方文檔(docs/reference/android/webkit/WebView.html)
1.載入URL對應的頁面
WebView webview = (WebView) findViewById(R.id.webview);...webview.loadUrl("www.csdn.net");
2.載入HTML代碼
WebView提供了loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl)方法,參數說明如下:
baseUrl:作為網頁輸入的網址(一般null)data:需要載入的HTML代碼mimeType:指定HTML代碼的MIME類型,HTML可指定為text/htmlencoding:指定HTML代碼的字元編碼historyUrl:曆史輸入的網址(一般null)
WebView webview = (WebView) findViewById(R.id.webview);StringBuilder sb = new StringBuilder();// 拼接一段HTML代碼sb.append("<html>");sb.append("<head>");sb.append("<title> Test </title>");sb.append("</head>");sb.append("<body>");sb.append("<h2> Test Page </h2>");sb.append("</body>");sb.append("</html>");// 使用簡單的loadData方法會導致亂碼// webview.loadData(sb.toString(), "text/html", "utf-8");// 載入、並顯示HTML代碼webview.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8", null);
3.通過WebView和js的互動
見文章:http://blog.csdn.net/smartbetter/article/details/51089613
Android開發實踐 WebView