Android UI WebView的使用:

來源:互聯網
上載者:User

標籤:

Android UI WebView的使用:

/**
* @author smiling
* @date 2016/10
*/

布局:


<?xml version="1.0" encoding="utf-8"?>
<WebView
  android:id="@+id/webview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"/>

//如果你想要載入的頁面中用了JavaScript,你必須為你的WebView使能JavaScript。
mVewView.getSettings().setJavaScriptEnabled(true);
//縮放,設定為不能縮放可以防止頁面上出現放大和縮小的表徵圖
mVewView.getSettings().setBuiltInZoomControls(false);
//緩衝
mVewView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
//開啟DOM storage API功能
mVewView.getSettings().setDomStorageEnabled(true);
//開啟application Cache功能
mVewView.getSettings().setAppCacheEnabled(false);
//不調用第三方瀏覽器即可進行頁面反應
mVewView.setWebViewClient(new WebViewClient() {
  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
    mVewView.loadUrl(url);
    return true;
  }

  @Override
  public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);
    progressDialog.dismiss();
  }
});

mVewView.loadUrl(“http://www.google.com“);

mVewView.loadUrl(“file:///android_asset/XX.html“);

String htmlString = "<h1>Title</h1><p>This is HTML text<br /><i>Formatted in italics</i><br />Anothor Line</p>";
// 載入這個html頁面
myWebView.loadData(htmlString, "text/html", "utf-8");

goBack() 和 goForward():當你的WebView覆寫了URL載入的行為,它會自動地對訪問過的網頁積累一個曆史;

/**
* 按鍵響應,在WebView中查看網頁時,按返回鍵的時候按瀏覽曆史退回,
* 如果不做此項處理則整個WebView返回退出
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
  // Check if the key event was the Back button and if there‘s history
  if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack())
  {
    // 返回鍵退回
    myWebView.goBack();
  return true;
  }
  // If it wasn‘t the Back key or there‘s no web page history, bubble up
  // to the default
  // system behavior (probably exit the activity)
  return super.onKeyDown(keyCode, event);
}

Android UI 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.