html與Android——webView

複製代碼
 1 <html>  2 <head>  3 <title>myHtml.html</title>  4  5 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  6 <meta http-equiv="description" content="this is my page">  7 <meta http-equiv="content-type" content="text/html; charset=UTF-8">  8  9 <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> 10 <!-- 此處編寫JavaScript --js --> 11 <script language="javascript"> 12 //定義函數,驗證表單信息是否合法 13 function checkAll(ele){ 14  alert(ele); 15 //驗證用戶名不爲空 16 var name = document.getElementById("id1");//獲得用戶名輸入框對象 17 var nameValue = name.value;//獲得具體的值 18 if(nameValue.length<=0){//驗證 19 // document.getElementById("div1").innerHTML="<font size='4' color='red'>用戶名不能爲空</font>"; 20  alert("用戶名不能爲空,請填寫"); 21 return; 22  }else{ 23  document.getElementById("div1").innerHTML=""; 24  } 25 //驗證密碼 26 var pass = document.getElementById("id2");//獲得密碼輸入框對象 27 var passValue = pass.value; 28 if(passValue.length < 6){ 29 // document.getElementById("div2").innerHTML="<font size='4' color='red'>密碼長度不能小於6位</font>"; 30  alert("密碼長度不能小於6位"); 31 return; 32  }else{ 33  document.getElementById("div2").innerHTML=""; 34  } 35 //驗證郵箱 36 var email = document.getElementById("id3");//獲得郵箱輸入框對象 37 var emailValue = email.value; 38 if(emailValue.indexOf("@") == -1){ 39 //普通的js代碼能夠經過設置WebSetting實現有效 40 // document.getElementById("div3").innerHTML="<font size='4' color='red'>郵箱不包含@!</font>"; 41 alert("郵箱不包含@,請檢查!");//經過webchromClient設置有效 42 return; 43 }else{ 44 document.getElementById("div3").innerHTML=""; 45 } 46 47 //輸入項都符合要求,提交表單,可是作一個確認的提醒 48 if(confirm("確認提交嗎?")){ 49 // document.getElementById("form1").submit(); 50 //把表單中的信息傳遞到android代碼中,經過調用android傳遞過來的對象myclass 51 window.myclass.sendData(nameValue,passValue,emailValue); 52 } 53 54 } 55 56 </script> 57 58 </head> 59 60 <body> 61 <form id="form1" action="success.html"> 62 <center> 63 <table > 64 <tr align="center"><td colspan="2"><a href="http://www.baidu.com">百度</a></td></tr> 65 <tr><td>用戶名:</td><td width="150"><input type="text" id="id1" name="username"/><div style="display:inline" id="div1"></div></td></tr> 66 <tr><td>密碼:</td><td width="150"><input type="password" id="id2" name="userpass"/><div style="display:inline" id="div2"></div></td></tr> 67 <tr><td>郵箱:</td><td width="150"><input type="text" id="id3" name="email"/><div style="display:inline" id="div3"></div></td></tr> 68 <tr ><td align="center" colspan="2"><input type="button" onclick="checkAll('開始驗證');" value="提交(app)"/></td></tr> 69 </table> 70 <center> 71 </form> 72 </body> 73 </html>
複製代碼
複製代碼
  1 package com.ch.day9_webviewdemo;  2  3 import java.io.UnsupportedEncodingException;  4 import java.net.URLEncoder;  5 import java.security.spec.EncodedKeySpec;  6  7 import android.os.Bundle;  8 import android.app.Activity;  9 import android.app.AlertDialog;  10 import android.content.Context;  11 import android.content.DialogInterface;  12 import android.util.Log;  13 import android.util.Xml.Encoding;  14 import android.view.Menu;  15 import android.view.View;  16 import android.view.View.OnClickListener;  17 import android.webkit.JavascriptInterface;  18 import android.webkit.JsResult;  19 import android.webkit.WebChromeClient;  20 import android.webkit.WebSettings;  21 import android.webkit.WebView;  22 import android.webkit.WebViewClient;  23 import android.widget.Button;  24 import android.widget.ExpandableListView;  25 import android.widget.TextView;  26 import android.widget.Toast;  27  28 public class MainActivity extends Activity {  29  private Button goback;  30  private Button tj;  31  public static final String HTML_URL = "http://169.254.70.111:8080/serverof1407a/myHtml.html";  32  33  private TextView tv;  34  private WebView wv;  35  Context mcontext;  36  37  class MyClass1{  38  @JavascriptInterface  39  public void sendData(String name,String pass,String email){  40  Toast.makeText(mcontext, name+","+pass+","+email, 0).show();  41  //網絡鏈接,發送到服務器保存,同時也能夠保存到本地sqlite  42  43  44  45  //跳轉到註冊成功頁面  46 // Intent it = new Intent(mcontext,第二個頁面);  47  }  48  }  49  50  @Override  51  protected void onCreate(Bundle savedInstanceState) {  52  super.onCreate(savedInstanceState);  53  setContentView(R.layout.activity_main);  54  mcontext = this;  55  init();  56  }  57  58  public void init(){  59  wv = (WebView) findViewById(R.id.wv);  60  goback = (Button) findViewById(R.id.goback);  61  tj = (Button) findViewById(R.id.tj);  62  //點擊 提交,經過android調用js的checkAll函數 63 tj.setOnClickListener(new OnClickListener() { 64 @Override 65 public void onClick(View v) { 66 wv.loadUrl("javascript:checkAll('android調用的驗證')"); 67 } 68 });  69  70  WebSettings setting = wv.getSettings();//得到websetting,設置一些參數  71  setting.setDefaultTextEncodingName("utf-8");//設置編碼,解決亂碼
72 //設置普通js有效 73 setting.setJavaScriptEnabled(true); 74 75 //第一種方式加載現成的html(兩個來源:網絡+本地) 76 // wv.loadUrl(HTML_URL);//加載服務器的html資源 77 wv.loadUrl("file:///android_asset/myHtml.html");//加載本地assets下的html 78 //向js中推入一個對象,供調用 79 wv.addJavascriptInterface(new MyClass1(), "myclass");
//解析數據後加載頁面 80 // webView.loadData(string, "text/html;charset=utf-8", "utf-8");
81 //監聽webview的url的從新定位 82 wv.setWebViewClient(new WebViewClient(){ 83 @Override 84 public boolean shouldOverrideUrlLoading(WebView view, String url) { 85 // TODO Auto-generated method stub 86 Log.i("TAG", "當前webview加載的新url:::"+url); 87 // if(url.equals("http://www.baidu.com/")){ 88 // wv.loadUrl("http://www.jd.com"); 89 // } 90 return super.shouldOverrideUrlLoading(view, url); 91 92 } 93 }); 94 //處理alert無效 95 wv.setWebChromeClient(new WebChromeClient(){ 96 //處理alert無效,當js彈出alert框的時候,會調用這個方法 97 @Override 98 public boolean onJsAlert(WebView view, String url, String message, 99 JsResult result) { 100 // TODO Auto-generated method stub 101 //用dialog對話框替代系統自帶的alert彈出 102 result.cancel();//關閉系統和自帶的alert警告框 103 //建立android的對話框替換 104 AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); 105 dialog.setTitle("警告框").setIcon(R.drawable.ic_launcher).setMessage(message) 106 .setPositiveButton("肯定", null) 107 .create().show(); 108 109 return true; 110 } 111 //處理confirm無效,當js彈出confirm框的時候,會調用這個方法 112 @Override 113 public boolean onJsConfirm(WebView view, String url, 114 String message, final JsResult result) { 115 // TODO Auto-generated method stub 116 117 //建立android的對話框替換 118 AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); 119 dialog.setTitle("確認提示框").setIcon(R.drawable.ic_launcher).setMessage(message) 120 .setPositiveButton("肯定", new DialogInterface.OnClickListener(){ 121 @Override 122 public void onClick(DialogInterface dialog, int which) { 123 result.confirm();//點擊dailog的確認,提交系統的確認框 124 } 125 }) 126 .setNegativeButton("取消", new DialogInterface.OnClickListener(){ 127 @Override 128 public void onClick(DialogInterface dialog, int which) { 129 result.cancel();//點擊dailog的取消,取消系統的確認框 130 } 131 }) 132 .create().show(); 133 return true; 134 } 135 }); 136 137 // 138 goback.setOnClickListener(new OnClickListener() { 139 @Override 140 public void onClick(View v) { 141 if(wv.canGoBack()){ 142 wv.goBack();//後退 143 // wv.goForward();//前進 144 // wv.zoomIn();//放大 145 // wv.zoomOut();//縮小 146 } 147 148 } 149 }); 150 151 152 153 // //第二種方式加載現拼寫的html 154 // StringBuffer sb = new StringBuffer(); 155 // sb.append("<html><head></head><body><input type='submit' value='提交(app'/></body></html>"); 156 // wv.loadData(sb.toString(), "text/html", "utf-8"); 157 } 158 159 @Override 160 public boolean onCreateOptionsMenu(Menu menu) { 161 // Inflate the menu; this adds items to the action bar if it is present. 162 getMenuInflater().inflate(R.menu.activity_main, menu); 163 return true; 164 } 165 166 }
複製代碼
相關文章
相關標籤/搜索