当前位置:编程学习 > JAVA >>

android中使用HTML作布局文件以及调用Javascript

    在android开发中,通常使用xml格式来描述布局文件,采用Android的layout布局有时候根本满足不了我们对于界面的要求,有时候没有web页面那样炫。就目前而言,熟悉android布局及美化的人员少之又少,出现了严重的断层。大部分企业,其实还是程序员自己动手布局。这样既浪费时间和精力,也未必能达到理想的效果。但是,在企业级的android开发中,使用html页面进行布局,也有很多的优势(例如:简单,大部分开发人员及美工都熟悉,方便统一进行更新,管理)。据笔者了解,已经有不少的公司在使用这种方式进行布局开发。这也可能是一种趋势。
下面,我将给出一个实例代码,供大家学习使用html页面给android应用布局。

Step 1 :新建一个Android工程,命名为HtmlForUI

 \

Step 2:在assets目录下写一个android.html文件,代码如下:

[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); //   将字符串string转换成json 
            var table = document.getElementById("personTable"); 
            for(var y=0; y<jsonobjs.length; y++){ 
                var tr = table.insertRow(table.rows.length); //添加一行 
                //添加3列 
                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> 
<!-- 调用WebView设置的    webView.addJavascriptInterface(new JSObject(), "contact");   插件contact中的java代码 --> 
<body onload="javascript:contact.showcontacts()"> 
   <table border="1" 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> 
    <a href="javascript:contact.startNewActivity()">跳转</a> 
</body> 
 
</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); //   将字符串string转换成json
         var table = document.getElementById("personTable");
         for(var y=0; y<jsonobjs.length; y++){
          var tr = table.insertRow(table.rows.length); //添加一行
          //添加3列
          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>
<!-- 调用WebView设置的 webView.addJavascriptInterface(new JSObject(), "contact");   插件contact中的java代码 -->
<body onload="javascript:contact.showcontacts()">
   <table border="1" 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>
 <a href="javascript:contact.startNewActivity()">跳转</a>
</body>

</html>
Step 3:编写该应用用到的用户实体类Contact.java和业务逻辑类 ContactService.java的代码如下:

Contact.java

[java]
package cn.roco.domain; 

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,