您的位置:首页 > 移动开发 > Android开发

使用网页开发软件界面

2012-11-26 14:43 417 查看
使用网页开发软件界面
 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);
         
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android JAVA