Android開發實踐 WebView

來源:互聯網
上載者:User

標籤:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.