今天在學習Android的過程中突然發現Android界面可以使用HTML和JavaScript來製作,立馬看了這個教程,順便記錄一下(代碼也是教程裏面的代碼)。
1.首先來看一下這個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=UTF-8">
<title>Insert title here</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");
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>
<!-- js代碼通過webView調用其插件中的java代碼 -->
<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>
這個HTML頁面很簡單,就是一個表格,js部分就是往表格裏面添加元素,不明白的地方可能就是這個contact是什麼了,這裏的contact就是用java代碼自定義的一個JavaScript對象,call()和showcontacts()就是這個對象中的兩個方法了
2.研究一下activity
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");
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();
}
}
}
}
webView.loadUrl("file:///android_asset/index.html");指定加載了一個HTML頁面,這個HTML頁面可以是本地的也可以是遠程的,如果是遠程的就可以隨時更改這個HTML頁面了,有時候會有這個需求,缺點是加載可能會慢一點,要耗流量
webView.getSettings().setJavaScriptEnabled(true);設置了JavaScript的運行權限
webView.addJavascriptInterface(new JSObject(), "contact");就是上面提到的contact對象的創建
contactService = new ContactService();創建數據
webView.loadUrl("javascript:show('"+ json+ "')");執行JavaScript的show()方法
3.貼上xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webView"
/>
</LinearLayout>
4.最後貼上創建數據類
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;
}
}
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;
}
}