安卓xml布局没有HTML方便,Android_如何在android中使用html作布局文件,在android开发中,通常使用xml格 - phpStudy...

package com.dazhuo.ui;

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内置浏览器对象

webview.getSettings().setJavaScriptEnabled(true);//启用javascript支持

//添加一个js交互接口,方便html布局文件中的javascript代码能与后台java代码直接交互访问

webview.addJavascriptInterface(new PersonPlugin() , "Person");//new类名,交互访问时使用的别名

//

webview.loadUrl("file:///android_asset/index.html");//加载本地的html布局文件

//其实可以把这个html布局文件放在公网中,这样方便随时更新维护  例如 webview.loadUrl("www.xxxx.com/index.html");

}

//定义一个内部类,从java后台(可能是从网络,文件或者sqllite数据库) 获取List集合数据,并转换成json字符串,调用前台js代码

private final class PersonPlugin{

public void getPersonList(){

List list = service.getPersonList();//获得List数据集合

//将List泛型集合的数据转换为JSON数据格式

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();//转换成json字符串

webview.loadUrl("javascript:show('"+ JSONStr +"')");//执行html布局文件中的javascript函数代码--

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);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值