使用網頁開發軟件界面
使用網頁開發軟件界面
Android通過webview實現了js代碼與java代碼互相通信的功能.
使得Android軟件的界面也可以採用HTML網頁技術.
只需改變服務器端的代碼.客戶端不需要更改即可實現界面的更改變化.
首先在佈局文件中設置佈局:
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webview"/>
然後在主Activity中設置顯示和點擊
public class MainActivity extends Activity {
public static final String path = "http://192.168.1.100:8080/day_test/index.html";
WebView webview ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) this.findViewById(R.id.webview);
//獲取webview的控制信息
WebSettings setting = webview.getSettings();
setting.setJavaScriptEnabled(true);//這個設置爲true才能執行javascript
//設置javascript的點擊事件
webview.addJavascriptInterface(new Object(){
public void call(String phone){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+phone));
startActivity(intent);
}
//調用了javascript的方法來顯示
public void showcontacts(){
//[{name:"xxx",amount:600,phone:"13988888"},{name:"bb",amount:200,phone:"1398788"}]
//該數據是通過新建一個Info類來模擬的,一般的是通過數據庫或者xml文件或者網絡來獲取.
// 調用網頁中的javascript 的方法 讓數據顯示到界面上
List<Info> infos = new ArrayList<Info>();
Info info1 = new Info("zhangsan", "12345", "1000");
Info info2 = new Info("zhangsan1", "123456", "2000");
Info info3 = new Info("zhangsan2", "123457", "3000");
infos.add(info1);
infos.add(info2);
infos.add(info3);
try {
JSONArray array = new JSONArray();
for(Info info : infos){
JSONObject object = new JSONObject();
object.put("name", info.getName());
object.put("amount", info.getAccout());
object.put("phone", info.getPhone());
array.put(object);
}
String json = array.toString();
webview.loadUrl("javascript:show('"+json+"')");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, "contact");
webview.loadUrl(path);
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.