因为android软件开发分工目前还没有细化,程序员往往需要负责软件界面的开发,虽然软件的界面图片已经由美工设计好了,但如果使用layout技术把软件做成如图片所示的界面确实很困难,而且也比较耗时。Android通过WebView实现了JS代码与Java代码互相通信的功能,使的android软件的界面开发也可以采用HTML网页技术,这样,广大网页美工可以参与进android软件的界面开发工作,从而让程序员从中解脱出来。
在项目的assets目录放入index.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 <style type="text/css">
7 A {
8 COLOR: #FFFFFF; TEXT-DECORATION: none
9 }
10 </style>
11 <script type="text/javascript">
12 function show(jsondata){
13 var jsonobjs = eval(jsondata);
14 var table = document.getElementById("personTable");
15 for(var y=0; y<jsonobjs.length; y++){
16 var tr = table.insertRow(table.rows.length); //添加一行
17 //添加三列
18 var td1 = tr.insertCell(0);
19 var td2 = tr.insertCell(1);
20 td2.align = "center";
21 var td3 = tr.insertCell(2);
22 //设置列内容和属性
23 td1.innerHTML = jsonobjs[y].id;
24 td2.innerHTML = "<a href='javascript:itcast.call(\"5554\")'>"+ jsonobjs[y].name + "</a>";
25 td3.innerHTML = jsonobjs[y].phone;
26 }
27 }
28 </script>
29 </head>
30 <body bgcolor="#000000" text="#FFFFFF" style="margin:0 0 0 0" onload="javascript:itcast.personlist()">
31 <table border="0" width="100%" id="personTable" cellspacing="0">
32 <tr>
33 <td width="15%">编号</td><td align="center">姓名</td><td width="15%">电话</td>
34 </tr>
35 </table>
36 <a href="javascript:window.location.reload()">刷新</a>
37 </body>
38 </html>
1 public class HtmlActivity extends Activity {
2 private WebView webView;
3 private Handler handler = new Handler();
4
5 @Override
6 public void onCreate(Bundle savedInstanceState) {
7 super.onCreate(savedInstanceState);
8 setContentView(R.layout.main);
9
10 webView = (WebView)this.findViewById(R.id.webView);
11 webView.getSettings().setJavaScriptEnabled(true);
12 webView.getSettings().setSaveFormData(false);
13 webView.getSettings().setSavePassword(false);
14 webView.getSettings().setSupportZoom(false);
15 webView.addJavascriptInterface(new ItcastJavaScript(), “itcast”);//addJavascriptInterface方法中要绑定的Java对象
16 webView.setWebChromeClient(new ItcastWebClient());
17 webView.loadUrl("file:///android_asset/index.html");
18 }
19
20 private final class ItcastJavaScript{
21 public void personlist(){
22 webview.loadUrl("javascript:contactlist('"+ getPersonJson() + "')");
23 }
24
25 public void call(final String phone){
26 startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ phone)));
27 }
28 public static String getPersonJson() {//生成json字符串
29 try {
30 JSONObject jsonObject = new JSONObject();
31 jsonObject.put("id", 56);
32 jsonObject.put("name", "老张");
33 jsonObject.put("phone", "5556");
34 JSONObject jsonObject2 = new JSONObject();
35 jsonObject2.put("id", 89);
36 jsonObject2.put("name", "老方");
37 jsonObject2.put("phone", "5558");
38 JSONArray jsonArray = new JSONArray();
39 jsonArray.put(jsonObject);
40 jsonArray.put(jsonObject2);
41 return jsonArray.toString();
42 } catch (JSONException e) {
43 e.printStackTrace();
44 }
45 return "";
46 }
47 }
48 private final class ItcastWebClient extends WebChromeClient{
49 @Override
50 public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
51 new AlertDialog.Builder(HtmlActivity.this)
52 .setTitle("提示信息")
53 .setMessage(message)
54 .setPositiveButton("确定", new DialogInterface.OnClickListener(){
55 public void onClick(DialogInterface dialoginterface, int i){}
56 }).show();
57 return true;
58 }
59 }
60 }