WebView控件

WebView:可以直接加载网页。

webview 可以把一个java对象传递给网页,让javascript调用这个对象里面的方法。

具体示例demo   html代码:

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 5 <title>Insert title here</title>
 6 <script type="text/javascript">
 7     function show(jsondata){//  [{name:"xxx",amount:600,phone:"13988888"},{name:"bb",amount:200,phone:"1398788"}]
 8             var jsonobjs = eval(jsondata);
 9             var table = document.getElementById("personTable");
10             for(var y=0; y<jsonobjs.length; y++){
11                 var tr = table.insertRow(table.rows.length); //添加一行
12                 //添加三列
13                 var td1 = tr.insertCell(0);
14                 var td2 = tr.insertCell(1);
15                 td2.align = "center";
16                 var td3 = tr.insertCell(2);
17                 td3.align = "center";
18                 //设置列内容和属性
19                 td1.innerHTML = jsonobjs[y].name; 
20                 td2.innerHTML = jsonobjs[y].amount; 
21                 td3.innerHTML = "<a href='javascript:contact.call(\""+ jsonobjs[y].phone+ "\")'>"+ jsonobjs[y].phone+ "</a>"; 
22             }
23     }
24 </script>
25 
26 </head>
27 <!-- js代码通过webView调用其插件中的java代码 -->
28 <body onload="javascript:contact.showcontacts()">
29    <table border="0" width="100%" id="personTable" cellspacing="0">
30         <tr>
31             <td width="35%">姓名</td><td width="30%" align="center">存款</td><td align="center">电话</td>
32         </tr>
33     </table>
34     <a href="javascript:window.location.reload()">刷新</a>
35 </body>
36 
37 </html>

布局文件:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical"
 5     >
 6 
 7     <WebView
 8         android:id="@+id/webview"
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent"
11         />
12 
13 </LinearLayout>

MainActivity代码:

 1 package com.android.hzy.webview;
 2 
 3 import java.util.List;
 4 
 5 import org.json.JSONArray;
 6 import org.json.JSONException;
 7 import org.json.JSONObject;
 8 
 9 import android.app.Activity;
10 import android.content.Intent;
11 import android.net.Uri;
12 import android.os.Bundle;
13 import android.webkit.WebView;
14 
15 import com.android.hzy.domain.ContactInfo;
16 import com.android.hzy.service.ContactService;
17 
18 public class MainActivity extends Activity {
19 
20     private WebView webview;
21     private ContactService service;
22 
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.activity_main);
27 
28         webview = (WebView) findViewById(R.id.webview);
29         service = new ContactService();
30 
31         // 给webview传递一个java对象
32         webview.addJavascriptInterface(new ContactPulgin(), "contact");
33         // webview能够执行javascript代码
34         webview.getSettings().setJavaScriptEnabled(true);
35 
36         // 加载本地网页
37 //        webview.loadUrl("file:///android_asset/index.html");
38         // 加载网页
39         webview.loadUrl("http://192.168.1.102:8080/web/");
40     }
41 
42     private final class ContactPulgin {
43         // 获取联系人的数据 javascript代码调用java代码
44         public void showcontacts() {
45             try {
46                 List<ContactInfo> contacts = service.getContact();
47 
48                 JSONArray jsonArray = new JSONArray();
49                 for (int i = 0; i < contacts.size(); i++) {
50                     JSONObject jsonObject = new JSONObject();
51                     ContactInfo info = contacts.get(i);
52                     jsonObject.put("name", info.getName());
53                     jsonObject.put("amount", info.getAmount());
54                     jsonObject.put("phone", info.getPhone());
55                     jsonArray.put(jsonObject);
56                 }
57                 String json = jsonArray.toString();
58                 // 在前面必须加上javascript: 说明是调用的javascript代码
59                 webview.loadUrl("javascript:show(" + json + ")");
60             } catch (Exception e) {
61                 // TODO Auto-generated catch block
62                 e.printStackTrace();
63             }
64         }
65         
66         public void call(String phone){
67             Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone));
68             startActivity(intent);
69         }
70     }
71 
72 }

模拟的数据:

 1 package com.android.hzy.service;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import com.android.hzy.domain.ContactInfo;
 7 /**
 8  * 模拟的数据
 9  */
10 public class ContactService {
11 
12     public List<ContactInfo> getContact() {
13 
14         List<ContactInfo> contactInfos = new ArrayList<ContactInfo>();
15         contactInfos.add(new ContactInfo("小强", 10000, "13111111111"));
16         contactInfos.add(new ContactInfo("小明", 20000, "13222222222"));
17         contactInfos.add(new ContactInfo("小红", 30000, "13333333333"));
18         contactInfos.add(new ContactInfo("小白", 40000, "13444444444"));
19         contactInfos.add(new ContactInfo("小东", 50000, "13555555555"));
20         contactInfos.add(new ContactInfo("小西", 60000, "13666666666"));
21         return contactInfos;
22     }
23 }
 1 package com.android.hzy.domain;
 2 
 3 public class ContactInfo {
 4 
 5     private String name;
 6     private long amount;
 7     private String phone;
 8 
 9     public ContactInfo(String name, long amount, String phone) {
10         super();
11         this.name = name;
12         this.amount = amount;
13         this.phone = phone;
14     }
15 
16     public String getName() {
17         return name;
18     }
19 
20     public void setName(String name) {
21         this.name = name;
22     }
23 
24     public long getAmount() {
25         return amount;
26     }
27 
28     public void setAmount(long amount) {
29         this.amount = amount;
30     }
31 
32     public String getPhone() {
33         return phone;
34     }
35 
36     public void setPhone(String phone) {
37         this.phone = phone;
38     }
39 
40     @Override
41     public String toString() {
42         return "ContactInfo [name=" + name + ", amount=" + amount + ", phone="
43                 + phone + "]";
44     }
45 
46 }

【注】:在2.3.3模拟器中,用webview去调用javascript代码会直接挂掉,应用退出,这个是BUG,在2.2以及别的版本不会有此问题,真机2.3.3木有测试!

 

posted @ 2013-02-08 17:34  My_苦行僧  阅读(1000)  评论(0编辑  收藏  举报