有关Android结合html/js/css的使用


下面的说明转自网上,是一个朋友自己学习的经验分享吧。我贴出来,大家多多注意,少犯错误。

重要说明:

android中的关键代码:

webview.getSettings().setJavaScriptEnabled(true);

webview.addJavascriptInterface(object,"name");//把Name="name"的对象添加到object中。object如果是this,就是window.name

webview.loadUrl("file:///android_asset/index.html");//注意这个资源的位置是跟res并列的文件夹下。

js或html中调用android中方法代码:

         如:

         或js中 window.name.java中的方法();

android中调用js的function方法:

         Callfunction(){

      webview.loadUrl("javascript: function ()");

}

 

需要注意的地方,很多数据类型js中不认识,最好是在android那边封装好,提供必要的方法接口。比如传到js中的list,在js中是没办法去得到里面的元素的。

addJavascriptInterface:addJavascriptInterface方法中要绑定的Java对象及方法要运行在另外的线程中,不能运行在构造他的线程中,也就是说不能运行在当前的activity线程中,就是把这个方法绑定到页面中,js也可以调用

 

文档中的解释:

Use this function to bind an object to Javascript so that the methods can be accessed from Javascript.

The Java object that is bound runs in another thread and not in the thread that it was constructed in.

其实上边都是废话:

重点是:以下两个连接

都是参考他们的:

1:.http://ysongren.blog.163.com/blog/static/6154220720109211020410/

2. http://blog.csdn.net/hanyuwei0/article/details/6541531

 

 

posted @ 2012-03-16 16:40  3H  阅读(1713)  评论(0编辑  收藏  举报