Android UI WebView的使用:html
/**
* @author smiling
* @date 2016/10
*/
佈局:android
<?xml version="1.0" encoding="utf-8"?>
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>web
//若是你想要載入的頁面中用了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「);app
mVewView.loadUrl(「file:///android_asset/XX.html「); ide
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載入的行爲,它會自動地對訪問過的網頁積累一個歷史;ui
/**
* 按鍵響應,在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);
}google