smiling1990

博客园 首页 联系 订阅 管理

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);
}

posted on 2016-10-14 09:11  smiling1990  阅读(173)  评论(0编辑  收藏  举报