在android開發(fā)中,通常使用xml格式來描述布局文件。就目前而言,熟悉android布局及美化的人員少之又少,出現(xiàn)了嚴重的斷層。大部分企業(yè),其實還是程序員自己動手布局。這樣既浪費時間和精力,也未必能達到理想的效果。但是,在企業(yè)級的android開發(fā)中,使用html頁面進行布局,也有很多的優(yōu)勢(例如:簡單,大部分開發(fā)人員及美工都熟悉,方便統(tǒng)一進行更新,管理)。據(jù)筆者了解,已經(jīng)有不少的公司在使用這種方式進行布局開發(fā)。這也可能是一種趨勢。
下面,我將給出一個實例代碼,供大家學(xué)習(xí)使用html頁面給android應(yīng)用布局。
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.dazhuo.domain.Person;
import com.dazhuo.service.PersonService;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
public class MainActivity extends Activity {
private PersonService service;
private WebView webview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
service =new PersonService();
webview = (WebView) this.findViewById(R.id.webView);//android內(nèi)置瀏覽器對象
webview.getSettings().setJavaScriptEnabled(true);//啟用javascript支持
//添加一個js交互接口,方便html布局文件中的javascript代碼能與后臺java代碼直接交互訪問
webview.addJavascriptInterface(new PersonPlugin() , "Person");//new類名,交互訪問時使用的別名
// <body onload="javascript:Person.getPersonList()">
webview.loadUrl("file:///android_asset/index.html");//加載本地的html布局文件
//其實可以把這個html布局文件放在公網(wǎng)中,這樣方便隨時更新維護 例如 webview.loadUrl("www.xxxx.com/index.html");
}
//定義一個內(nèi)部類,從java后臺(可能是從網(wǎng)絡(luò),文件或者sqllite數(shù)據(jù)庫) 獲取List集合數(shù)據(jù),并轉(zhuǎn)換成json字符串,調(diào)用前臺js代碼
private final class PersonPlugin{
public void getPersonList(){
List<Person> list = service.getPersonList();//獲得List數(shù)據(jù)集合
//將List泛型集合的數(shù)據(jù)轉(zhuǎn)換為JSON數(shù)據(jù)格式
try {
JSONArray arr =new JSONArray();
for(Person person :list)
{
JSONObject json =new JSONObject();
json.put("id", person.getId());
json.put("name", person.getName());
json.put("mobile",person.getMobile());
arr.put(json);
}
String JSONStr =arr.toString();//轉(zhuǎn)換成json字符串
webview.loadUrl("javascript:show('"+ JSONStr +"')");//執(zhí)行html布局文件中的javascript函數(shù)代碼--
Log.i("MainActivity", JSONStr);
} catch (Exception e) {
// TODO: handle exception
}
}
//打電話的方法
public void call(String mobile){
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ mobile));
startActivity(intent);
}
}
}
public class Person {
private Integer id;
public Integer getId() {
return id;
}
public Person(Integer id, String name, String mobile) {
super();
this.id = id;
this.name = name;
this.mobile = mobile;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
private String name;
private String mobile;
}
import java.util.ArrayList;
import java.util.List;
import com.dazhuo.domain.Person;
public class PersonService {
public List<Person> getPersonList()
{
List<Person> list =new ArrayList<Person>();
list.add(new Person(32, "aa", "13675574545"));
list.add(new Person(32, "bb", "13698874545"));
list.add(new Person(32, "cc", "13644464545"));
list.add(new Person(32, "dd", "13908978877"));
list.add(new Person(32, "ee", "15908989898"));
return list;
}
}
</head>
<!-- js代碼通過webView調(diào)用其插件中的java代碼 -->
<body onload="javascript:Person.getPersonList()">
<table border="0" width="100%" id="personTable" cellspacing="0">
<tr>
<td width="20%">編號</td><td width="40%" align="center">姓名</td><td align="center">電話</td>
</tr>
</table>
<a href="javascript:window.location.reload()">刷新</a>
</body>
</html>
新聞熱點
疑難解答
圖片精選