码迷,mamicode.com
首页 > 编程语言 > 详细

WebView上实现Java与JavaScript交互

时间:2016-03-16 15:19:37      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

      在安卓开发上,考虑到开发效率和界面更新,有时使用WebView结合web页面技术,可以快速迭代地开发移动应用。WebView加载资源的速度并不慢,但是如果资源多了,就很慢。图片、css、js、html这些资源每个大概需要10-200ms,一般都是30ms以内就行了。但是,WebView是等全部资源加载完成才开始渲染的,所以最后用原生js来写,别用太多jQuery之类的框架,以改善用户体验。

  在混合开发中,有时会用到安卓原生SDK,如调用相机、查看相册、录音等,这就需要web页面中的JavaScript能调用到安卓SDK接口。由于Android的WebView是基于webkit内核的,集成了js与java互调的接口函数,可以方便地进行开发使用。

代码:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <WebView
 7         android:id="@+id/webView"
 8         android:layout_width="fill_parent"
 9         android:layout_height="fill_parent"
10         android:layout_above="@+id/linearLayout"
11          />
12     <LinearLayout 
13         android:id="@+id/linearLayout"
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:layout_alignParentBottom="true"
17         >
18         <Button
19             android:id="@+id/btn"
20             android:layout_width="wrap_content"
21             android:layout_height="wrap_content"
22             android:text="Java调用JavaScript接口"
23             >
24         </Button>
25     </LinearLayout>
26 </RelativeLayout>

 

WebView上实现Java与JavaScript交互

标签:

原文地址:http://www.cnblogs.com/chq3272991/p/5283667.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!