使用html开发软件界面

因为android软件开发分工目前还没有细化,程序员往往需要负责软件界面的开发,
虽然软件的界面图片已经由美工设计好了,但如果使用layout技术把软件做成如图片所示的界面确实很困难,
而且也比较耗时。Android通过WebView实现了JS代码与Java代码互相通信的功能,
使得android软件的界面开发也可以采用HTML网页技术,这样,广大网页美工可以参
与进android软件的界面开发工作,从而让程序员从中解脱出来。

代码中使用浏览器Webkit,就要使用到WebView控件;使用 显示网页内容;

采用布局layout完成软件界面很累人,很痛苦;

将网页文件及图片放到assets目录下


json数组[{id:20,name:"xxx",mobile:"1332"},{}]

json字符串转成json对象


java对象作为插件安装到webkit,js代码就能访问到数据了;


OO的方法将对象的数据转成json字符串;

 

配置单元测试:

 android:targetPackage="cn.itcast.html" android:label="My File Test" />


隐式意图Intent调用电话拨号器;

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mobile));
startActivity(intent);

拨打电话权限:

软件界面更新立刻被用户看到:将index.html放到服务器中;
webView.loadUrl("http://192.168.1.100:8080/web/index.html");

允许网络访问权限:

[html]
  1. index.html  
  2.   
  3. >  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title heretitle>  
  8. <script type="text/javascript">  
  9.     function show(jsondata){  
  10.             var jsonobjs = eval(jsondata);//json字符串转换为json对象  
  11.             var table = document.getElementById("personTable");  
  12.             for(var y=0; y<jsonobjs.length; y++){  
  13.                 var tr = table.insertRow(table.rows.length); //添加一行  
  14.                 //添加三列  
  15.                 var td1 = tr.insertCell(0);  
  16.                 var td2 = tr.insertCell(1);  
  17.                 td2.align = "center";  
  18.                 var td3 = tr.insertCell(2);  
  19.                 td3.align = "center";  
  20.                 //设置列内容和属性  
  21.                 td1.innerHTML = jsonobjs[y].id;   
  22.                 td2.innerHTML = jsonobjs[y].name;   
  23.                 td3.innerHTML = "<a href='javascript:contact.call(\""+ jsonobjs[y].mobile+ "\")'>"+ jsonobjs[y].mobile+ "a>";   
  24.             }  
  25.     }  
  26. script>  
  27.   
  28. head>  
  29.   
  30. <body onload="javascript:contact.getContacts()">  
  31.    <table border="0" width="100%" id="personTable" cellspacing="0">  
  32.         <tr>  
  33.             <td width="20%">编号td><td width="40%" align="center">姓名td><td align="center">电话td>  
  34.         tr>  
  35.     table>  
  36.     <a href="javascript:window.location.reload()">刷新a>  
  37. body>  
  38.   
  39. html>  
     
[html]
  1. Contact.java  
  2.   
  3. package cn.itcast.domain;  
  4.   
  5. public class Contact {  
  6.     private Integer id;  
  7.     private String name;  
  8.     private String mobile;  
  9.       
  10.     public Contact(Integer id, String name, String mobile) {  
  11.         this.id = id;  
  12.         this.name = name;  
  13.         this.mobile = mobile;  
  14.     }  
  15.     public Integer getId() {  
  16.         return id;  
  17.     }  
  18.     public void setId(Integer id) {  
  19.         this.id = id;  
  20.     }  
  21.     public String getName() {  
  22.         return name;  
  23.     }  
  24.     public void setName(String name) {  
  25.         this.name = name;  
  26.     }  
  27.     public String getMobile() {  
  28.         return mobile;  
  29.     }  
  30.     public void setMobile(String mobile) {  
  31.         this.mobile = mobile;  
  32.     }  
  33. }  

 

[html]
  1. ContactService.java  
  2.   
  3. package cn.itcast.service;  
  4.   
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. import cn.itcast.domain.Contact;  
  9.   
  10. public class ContactService {  
  11.       
  12.     public List<Contact> getContacts(){  
  13.         List<Contact> contacts = new ArrayList<Contact>();  
  14.         contacts.add(new Contact(2, "shenlu", "5201314"));  
  15.         contacts.add(new Contact(12, "shenlu", "5201314"));  
  16.         contacts.add(new Contact(22, "shenlu", "5201314"));  
  17.         return contacts;  
  18.     }  
  19. }  

 

[html]
  1. MainActivity.java  
  2.   
  3. package cn.itcast.html;  
  4.   
  5. import java.util.List;  
  6.   
  7. import org.json.JSONArray;  
  8. import org.json.JSONException;  
  9. import org.json.JSONObject;  
  10.   
  11. import cn.itcast.domain.Contact;  
  12. import cn.itcast.service.ContactService;  
  13. import android.app.Activity;  
  14. import android.content.Intent;  
  15. import android.net.Uri;  
  16. import android.os.Bundle;  
  17. import android.webkit.WebView;  
  18.   
  19. public class MainActivity extends Activity {  
  20.     private ContactService service;  
  21.     private WebView webView;  
  22.       
  23.     @Override  
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.main);  
  27.           
  28.         service = new ContactService();  
  29.         webView = (WebView) this.findViewById(R.id.webView);  
  30.         webView.getSettings().setJavaScriptEnabled(true);//允许webkit执行js代码;  
  31.         //对象,插件名称  
  32.         //window.open()  
  33.         //document.write()  
  34.         //contact.xxx  
  35.         webView.addJavascriptInterface(new ContactPlugin(), "contact");//为webkit添加插件,即java类  
  36.         webView.loadUrl("file:///android_asset/index.html");//加载html文件  
  37.         //webView.loadUrl("http://192.168.1.100:8080/web/index.html");  
  38.     }  
  39.       
  40.     private final class ContactPlugin{  
  41.         public void getContacts(){  
  42.             List<Contact> contacts = service.getContacts();  
  43.             //OO的方法将对象的数据转成json字符串;  
  44.             try {  
  45.                 JSONArray jsonArray = new JSONArray();//新建json数组  
  46.                 for(Contact contact : contacts){  
  47.                     JSONObject item = new JSONObject();//新建json对象  
  48.                     item.put("id", contact.getId());  
  49.                     item.put("name", contact.getName());  
  50.                     item.put("mobile", contact.getMobile());  
  51.                     jsonArray.put(item);  
  52.                 }  
  53.                 String json = jsonArray.toString();//得到json字符串;  
  54. //              webView.loadUrl("javascript:show('[{""},{}]')");  
  55.                 webView.loadUrl("javascript:show('"+ json +"')"); //调用js的show方法;  
  56.                   
  57.             } catch (JSONException e) {  
  58.                 e.printStackTrace();  
  59.             }  
  60.         }  
  61.           
  62.         public void call(String mobile){  
  63.             Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mobile));  
  64.             startActivity(intent);  
  65.         }  
  66.     }  
  67. }  


 

[html]
  1. ContactTest.java-----单元测试  
  2.   
  3. package cn.itcast.html;  
  4.   
  5. import java.util.List;  
  6.   
  7. import org.json.JSONArray;  
  8. import org.json.JSONException;  
  9. import org.json.JSONObject;  
  10.   
  11. import cn.itcast.domain.Contact;  
  12. import cn.itcast.service.ContactService;  
  13. import android.test.AndroidTestCase;  
  14. import android.util.Log;  
  15.   
  16. public class ContactTest extends AndroidTestCase {  
  17.     private static final String TAG = "MainActivity";  
  18.   
  19.     public void testContact() throws Exception{  
  20.         List<Contact> contacts = new ContactService().getContacts();  
  21.               
  22.         //OO的方法将对象的数据转成json字符串;  
  23.             JSONArray jsonArray = new JSONArray();//新建json数组  
  24.             for(Contact contact : contacts){  
  25.                 JSONObject item = new JSONObject();//新建json对象  
  26.                 item.put("id", contact.getId());  
  27.                 item.put("name", contact.getName());  
  28.                 item.put("mobile", contact.getMobile());  
  29.                 jsonArray.put(item);  
  30.             }  
  31.             String json = jsonArray.toString();//得到json字符串;  
  32.             Log.i(TAG, json);  
  33.     }  
  34.  
[html]
  1. AndroidManifest.xml  
  2.   
  3. <?xml version="1.0" encoding="utf-8"?>  
  4. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     package="cn.itcast.html"  
  6.     android:versionCode="1"  
  7.     android:versionName="1.0" >  
  8.   
  9.     <uses-sdk android:minSdkVersion="8" />  
  10.   
  11.     <application  
  12.         android:icon="@drawable/ic_launcher"  
  13.         android:label="@string/app_name" >  
  14.         <uses-library android:name="android.test.runner"/>  
  15.         <activity  
  16.             android:name=".MainActivity"  
  17.             android:label="@string/app_name" >  
  18.             <intent-filter>  
  19.                 <action android:name="android.intent.action.MAIN" />  
  20.   
  21.                 <category android:name="android.intent.category.LAUNCHER" />  
  22.             intent-filter>  
  23.         activity>  
  24.     application>  
  25.       
  26.     <uses-permission android:name="android.permission.CALL_PHONE"/>  
  27.     <uses-permission android:name="android.permission.INTERNET"/>  
  28.       
  29.     <instrumentation android:name="android.test.InstrumentationTestRunner"   
  30.         android:targetPackage="cn.itcast.html" android:label="My File Test" />  
  31.   
  32. manifest>  
[html]
  1. /layout/main.xml  
  2.   
  3. <?xml version="1.0" encoding="utf-8"?>  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <WebView  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:id="@+id/webView"  
  13.         />  
  14.   
  15. LinearLayout>  

更多相关文章

  1. 类和 Json对象
  2. Android应用程序apk内xml文件编码解析
  3. Android高手进阶教程(二十五)之---Android(安卓)中的AIDL!!!
  4. Android(安卓)实用工具Hierarchy Viewer实战
  5. Handler和Message的使用之二
  6. Android高手进阶教程(二十五)之---Android(安卓)中的AIDL!!!
  7. Android(安卓)获取网络时间
  8. 在Android中创建启动界面
  9. android 想要弹出另外界面操作步骤

随机推荐

  1. 配置 Lync 2010 for iPhone
  2. Android 中关于Cursor类的介绍
  3. Android 按钮点击事件监听的3重方式
  4. android 实现保存图片到相册
  5. android机制系列之七 Android Camera API
  6. 使用shape来定义控件的一些显示属性
  7. Android实现多线程下载图片的方法
  8. 使用Qt5.9开发Android 应用程序(Windows
  9. Android6.0修改设备设置中kernel信息
  10. 短视频PHP源码Android 打造专属的下拉刷