Android-WebView基本使用

來源:互聯網
上載者:User

標籤:android   webview   

小編一直任務將web和android組件結合起來做應用可以事半功倍,html5一來就更有說服力了,特別是對於以前從事web開發的兄弟來說

1. webview加入布局檔案:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/allPage"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 
   <WebView android:id="@+id/webview"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent">
      </WebView>
</RelativeLayout>

2. 重寫WebChromeClient或webClient,主要是針對提示框和返回鍵

public class WebBrowserClient extends WebChromeClient {
 String title = Constants.appname;
 @Override
 public void onCloseWindow(WebView window) {
  super.onCloseWindow(window);
 }

 @Override
 public boolean onCreateWindow(WebView view, boolean dialog,
   boolean userGesture, Message resultMsg) {
  return super.onCreateWindow(view, dialog, userGesture, resultMsg);
 }

 /**
  * 覆蓋預設的window.alert展示介面,避免title裡顯示為“:來自file:////”
  */
 public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
  final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
    
  builder.setTitle(title).setMessage(message).setPositiveButton("確定", null);
    
  // 不需要綁定按鍵事件
  // 屏蔽keycode等於84之類的按鍵
  builder.setOnKeyListener(new OnKeyListener() {
   public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
    Log.v("onJsAlert", "keyCode==" + keyCode + "event="+ event);
    return true;
   }
  });
  // 禁止響應按back鍵的事件
  builder.setCancelable(true);
  AlertDialog dialog = builder.create();
  dialog.show();
  result.confirm();// 因為沒有綁定事件,需要強行confirm,否則頁面會變黑顯示不了內容。
  return true;
  // return super.onJsAlert(view, url, message, result);
 }

3. HTML頁面裡面的定義:

  function gopage(url){
   window.androidMain.loadURL(url);
  }

4. Activity裡面的使用

 // 初始化WEB頁面
 private void setupViews() {
  mWebView.setHorizontalScrollBarEnabled(false);
  mWebView.setVerticalScrollBarEnabled(true);
  mWebView.setScrollbarFadingEnabled(true);
  mWebView.setWebChromeClient(new WebBrowserClient());
  mWebView.setInitialScale(1);
  mWebView.setLongClickable(false);
  
  WebSettings mWebSettings = mWebView.getSettings();
  mWebSettings.setJavaScriptEnabled(true);
  mWebSettings.setUseWideViewPort(true);
  mWebSettings.setLoadWithOverviewMode(true);
  mWebSettings.setUseWideViewPort(true);
  
  
  // 支援多點觸控縮放
  mWebSettings.setSupportZoom(false);
  mWebSettings.setBuiltInZoomControls(false);
  
  // 使用緩衝
  mWebSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
  mWebView.loadUrl("file:///android_asset/b.html");

  ObjectClassHere cc = new ObjectClassHere();
  mWebView.addJavascriptInterface(cc, "androidMain");
 }

 class ObjectClassHere {
  
  public void exitSystem(){
   finish();
   android.os.Process.killProcess(android.os.Process.myPid());
  }
  
  public void loadURL(String url){
   String furl = "file:///android_asset/" + url;
   mWebView.loadUrl(furl);
  }
  
  public void showChaPinAdvs(int cr){
   Log.v(tag, "111 showChaPinAdvs currLevel="+cr % 2);
   if((cr % 2) == 0){
    showadvs = true;
    SpotManager.getInstance(context).showSpotAds(context);
   }
  }

5. web頁面的開發週期和效果都要較activity快速,但目前的效果相對一般,有待改進

使用情境有很多,比如吹泡泡【http://zhushou.360.cn/detail/index/soft_id/341284】,泡泡的動畫用activity很難實現,但是用html+js就比較容易一些,結合activtiy對話筒的控制就可以做出來了。


 

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.