使用网页开发软件界面

使用网页开发软件界面
 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);
         
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值