Android 采用HTML设计界面
2015-06-11 19:22
405 查看
因为Android软件开发分工目前还没有细化,程序员往往需要负责软件界面的开发,虽然软件的界面图片已经由美工设计好了,但是如果使用layout技术把软件做成漂亮的界面确实很困难,而是也比较耗时。Android通过WebView实现了JS代码与Java代码互相通信的功能,使的Android软件的界面开发也可以采用HTML网页技术,这样,广大网页美工可以参与进Android软件的界面开发工作,从而让程序员从中解脱出来。
工程目录
main.xml
index.html
Contact.java
ContactService.java
MainActivity.java
测试结果
工程目录
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <WebView android:id="@+id/webView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/> <title>Wty htmlUI</title> <script type="text/javascript"> function show(jsondata){// [{name:"xxx",amount:600,phone:"13988888"},{name:"bb",amount:200,phone:"1398788"}] var jsonobjs = eval(jsondata); var table = document.getElementById("personTable");//找到personTable表 for(var y=0; y<jsonobjs.length; y++){ var tr = table.insertRow(table.rows.length); //追加一行 //添加三列 var td1 = tr.insertCell(0); var td2 = tr.insertCell(1); td2.align = "center"; var td3 = tr.insertCell(2); td3.align = "center"; //设置列内容和属性 td1.innerHTML = jsonobjs[y].name; td2.innerHTML = jsonobjs[y].amount; td3.innerHTML = "<a href='javascript:contact.call(\""+ jsonobjs[y].phone+ "\")'>"+ jsonobjs[y].phone+ "</a>"; } } </script> </head> <!-- 页面加载的时候触发onload方法:js代码通过webView调用其插件中的java代码 --> <!-- 自定义js对象contact,调用里面的showcontacts()方法 --> <body onload="javascript:contact.showcontacts()"> <table border="0" width="100%" id="personTable" cellspacing="0"> <tr> <td width="35%">姓名</td><td width="30%" align="center">存款</td><td align="center">电话</td> </tr> </table> <a href="javascript:window.location.reload()">刷新</a> </body> </html>
Contact.java
package cn.itcast.domain; public class Contact { private Integer id; private String name; private String phone; private Integer amount; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public Integer getAmount() { return amount; } public void setAmount(Integer amount) { this.amount = amount; } public Contact(Integer id, String name, String phone, Integer amount) { this.id = id; this.name = name; this.phone = phone; this.amount = amount; } }
ContactService.java
package cn.itcast.service; import java.util.ArrayList; import java.util.List; import cn.itcast.domain.Contact; public class ContactService { /** * 获取联系人 * @return */ public List<Contact> getContacts(){ List<Contact> contacts = new ArrayList<Contact>(); contacts.add(new Contact(12, "张明", "13766666666", 13003)); contacts.add(new Contact(23, "小小", "130066006", 122003)); contacts.add(new Contact(98, "李小楷", "186768768", 10988787)); contacts.add(new Contact(76, "赵得", "1565622566", 1666)); return contacts; } }
MainActivity.java
package cn.itcast.html; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import cn.itcast.domain.Contact; import cn.itcast.service.ContactService; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.webkit.WebView; public class MainActivity extends Activity { private WebView webView; private ContactService contactService; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); webView = (WebView) this.findViewById(R.id.webView); webView.loadUrl("file:///android_asset/index.html"); webView.getSettings().setJavaScriptEnabled(true); webView.addJavascriptInterface(new JSObject(), "contact");//js对象是 JSObject(),名称为contact contactService = new ContactService(); } private final class JSObject{ public void call(String phone){ Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ phone)); startActivity(intent); } public void showcontacts(){ // [{name:"xxx",amount:600,phone:"13988888"},{name:"bb",amount:200,phone:"1398788"}] try { List<Contact> contacts = contactService.getContacts(); JSONArray jsonArray = new JSONArray(); for(Contact c : contacts){ JSONObject jsonObject = new JSONObject(); jsonObject.put("name", c.getName()); jsonObject.put("amount", c.getAmount()); jsonObject.put("phone", c.getPhone()); jsonArray.put(jsonObject); } String json = jsonArray.toString(); webView.loadUrl("javascript:show('"+ json+ "')"); } catch (JSONException e) { e.printStackTrace(); } } } }
测试结果
相关文章推荐
- Android系统框架介绍和生命周期介绍
- Android Menu 布局
- Android中onActivityResult()获取返回值
- Android颜色透明度
- Android学习 4 -> 布局结构的学习
- Android Handler 异步消息处理机制的妙用 创建强大的图片加载类
- Update curl on Android to resolve security problem cause by OpenSSL
- Android圆形图片--自定义控件
- Android利用Volley异步加载数据(JSON和图片)完整示例
- android activities 切换出现白屏
- android 回调函数二:应用实例
- Android中Parcelable接口用法
- android_Environment类
- 一个在线看android系统各个版本源码的链接
- 如何在真机上调试Android应用程序(图文详解)
- Android权限和动作大全
- vs xamarin android 读取rest
- android:exported 属性详解
- Android学习之路2_五大布局
- #Hello,2015征文#Android资料指南