Chinaunix首页 | 论坛 | 博客
  • 博客访问: 295954
  • 博文数量: 53
  • 博客积分: 1266
  • 博客等级: 少尉
  • 技术积分: 572
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-16 16:45
文章分类

全部博文(53)

文章存档

2012年(37)

2011年(16)

分类: 嵌入式

2012-11-29 14:47:27

一、在AndroidManifest.xml中增加权限;
二、在assets文件下创建index.html文件,并写入以下内容

点击(此处)折叠或打开

  1. <html>
  2. <head>
  3. <title>WebView Test</title>
  4. <script type="text/javascript">
  5. var a = true;
  6. function useAndroid(){
  7.     window.testAndroid.clickOnAndroid();
  8. }

  9. function goActivity(){
  10.     window.testAndroid.goActivity();
  11. }

  12. function androidUseJs(){
  13.     var element = document.getElementById('accelerometer');
  14.     if(a){
  15.      element.innerHTML = '让Android激发的';
  16.      a = false;
  17.     }else{
  18.      element.innerHTML = '';
  19.      a = true;
  20.     }
  21. }
  22. </script>
  23. </head>
  24. <body>
  25. <p onclick='useAndroid();'>调用Android功能</p>
  26. <p onclick='goActivity();'>跳转Activity界面</p>
  27. <div id="accelerometer"></div>
  28. </body>
  29. </html>
三、设置主界面的布局文件

点击(此处)折叠或打开

  1. <RelativeLayout xmlns:android=""
  2.     xmlns:tools=""
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent" >

  5.     <WebView android:id="@+id/wv"
  6.     android:layout_width="fill_parent"
  7.     android:layout_height="fill_parent"
  8.     android:text="@string/hello_world"
  9.     />

  10.     <Button
  11.         android:id="@+id/but_js"
  12.         android:layout_width="wrap_content"
  13.         android:layout_height="wrap_content"
  14.         android:layout_alignParentBottom="true"
  15.         android:layout_alignParentLeft="true"
  16.         android:layout_marginLeft="46dp"
  17.         android:text="Android按钮执行js" />

  18. </RelativeLayout>
三、创建一个新的Activity,JaActivity

点击(此处)折叠或打开

  1. package com.example.webviewdemo;

  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;

  8. public class JaActivity extends Activity {

  9.     private Button btn_back;
  10.     private JaActivity self;

  11.     @Override
  12.     public void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.ja);
  15.         self = this;

  16.         btn_back = (Button) this.findViewById(R.id.btn_back);
  17.         btn_back.setOnClickListener(new OnClickListener() {

  18.             public void onClick(View v) {
  19.                 Intent it = new Intent(self, MainActivity.class);
  20.                 self.startActivity(it);
  21.                 self.finish();
  22.             }
  23.         });
  24.     }
  25. }
四、设置新创建Activity的布局,ja.xml

点击(此处)折叠或打开

  1. <RelativeLayout xmlns:android=""
  2.     xmlns:tools=""
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent" >

  5.     <TextView
  6.         android:id="@+id/textView1"
  7.         android:layout_width="wrap_content"
  8.         android:layout_height="wrap_content"
  9.         android:layout_alignParentTop="true"
  10.         android:layout_centerHorizontal="true"
  11.         android:layout_marginTop="47dp"
  12.         android:text="从WebView跳过来" />

  13.     <Button
  14.         android:id="@+id/btn_back"
  15.         android:layout_width="wrap_content"
  16.         android:layout_height="wrap_content"
  17.         android:layout_below="@+id/textView1"
  18.         android:layout_centerHorizontal="true"
  19.         android:layout_marginTop="91dp"
  20.         android:text="返回" />

  21. </RelativeLayout>
以上代码演示了以下功能:
1、javascript调用Android功能;
2、Android调用javascript功能;
3、WebView跳转到Android界面;
4、从Android界面跳转到WebView界面
阅读(7034) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~