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


【Java调用Javascript方法 Javascript调用Java方法】

在项目的assets目录放入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=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>




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+ "')");//调用javascript方法
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}

更多相关文章

  1. irrlicht移植到android
  2. android 自定义搜索 的简单实现
  3. Android(安卓)之 zygote 与进程创建
  4. Android(安卓)JNI(java native interface) 学习笔记
  5. Android(安卓)Service
  6. Android(安卓)切换系统语言源码分析
  7. Android网络相关
  8. Android(安卓)4.0用户界面设计和元素-ICS简介
  9. Android设计登陆界面

随机推荐

  1. linux下mysql开启远程访问权限及防火墙开
  2. 《MySQL基础知识》笔记
  3. SQL语句字符串
  4. 内网安装mysql主从服务,mysql版本5.6.38-w
  5. 查询从每个组中查找第二大值
  6. 2018年5月05日 Python3-Mysql连接
  7. 数据库Mysql的学习(一)
  8. 解决Myeclipse下Debug出现Source not fou
  9. ORACLE 两表关联更新
  10. MySQL乐观锁在分布式场景下的实践