Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3493948
  • 博文数量: 864
  • 博客积分: 14125
  • 博客等级: 上将
  • 技术积分: 10634
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-27 16:53
个人简介

https://github.com/zytc2009/BigTeam_learning

文章分类

全部博文(864)

文章存档

2023年(1)

2021年(1)

2019年(3)

2018年(1)

2017年(10)

2015年(3)

2014年(8)

2013年(3)

2012年(69)

2011年(103)

2010年(357)

2009年(283)

2008年(22)

分类: Java

2010-11-16 11:59:06

在Android中通过WebView控件,可以实现要加载的页面与Android方法相互调用,我们要实现WebView中的addJavascriptInterface方法,这样html才能调用android方法,在这里我个人觉得有点和DWR相似。

为了让大家容易理解,我写了一个简单的Demo,具体步骤如下:

第一步:新建一个Android工程,命名为WebViewDemo(这里我在assets里定义了一个html页面)。

第二步:修改main.xml布局文件,增加了一个WebView控件还有Button控件,代码如下:

  1. "1.0" encoding="utf-8"?>  
  2. ""  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     
  8.         android:layout_width="fill_parent"   
  9.         android:layout_height="wrap_content"   
  10.         android:text="Welcome to Mr Wei's Blog."  
  11.         />  
  12.     
  13.         android:id="@+id/webview"  
  14.         android:layout_width="fill_parent"   
  15.         android:layout_height="wrap_content"   
  16.     />  
  17.     
  18.         android:id="@+id/button"  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="Change the webview content"  
  22.     />  
  23.   

第三步:在assets目录下新建一个demo.html文件,代码如下(这里不知道为何多了mce:这几个东东,这样是对的):

  1.   
  2.     "javascript">    
  3.     
  4.     

    "window.demo.startMap()" href="">Start GoogleMap

      
  5.     "content">

      
  6.     

    A Demo ----Android and Javascript invoke each other.

      
  7.     

    Author:Frankiewei

      
  8.      
  9.    

第四步:修改主核心程序WebViewDemo.java,代码如下:

  1. package com.tutor.webwiewdemo;  
  2. import android.app.Activity;  
  3. import android.content.ComponentName;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.webkit.WebSettings;  
  8. import android.webkit.WebView;  
  9. import android.widget.Button;  
  10. public class WebViewDemo extends Activity {  
  11.     private WebView mWebView;  
  12.     private Button mButton;  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         setupViews();  
  17.     }  
  18.     //初始化  
  19.     private void setupViews() {  
  20.         mWebView = (WebView) findViewById(R.id.webview);  
  21.         WebSettings mWebSettings = mWebView.getSettings();  
  22.         //加上这句话才能使用javascript方法  
  23.         mWebSettings.setJavaScriptEnabled(true);  
  24.         //增加接口方法,让html页面调用  
  25.         mWebView.addJavascriptInterface(new Object() {  
  26.             //这里我定义了一个打开地图应用的方法  
  27.             public void startMap() {  
  28.                 Intent mIntent = new Intent();  
  29.                 ComponentName component = new ComponentName(  
  30.                         "com.google.android.apps.maps",  
  31.                         "com.google.android.maps.MapsActivity");  
  32.                 mIntent.setComponent(component);  
  33.                 startActivity(mIntent);  
  34.             }  
  35.         }, "demo");  
  36.         //加载页面  
  37.         mWebView.loadUrl("file:///android_asset/demo.html");  
  38.         mButton = (Button) findViewById(R.id.button);  
  39.         //给button添加事件响应,执行JavaScript的fillContent()方法  
  40.         mButton.setOnClickListener(new Button.OnClickListener() {  
  41.             public void onClick(View v) {  
  42.                 mWebView.loadUrl("javascript:fillContent()");  
  43.             }  
  44.         });  
  45.     }  
  46. }  

第五步:运行上述工程,查看效果。

  

                        首界面                                           点击按钮时,html内容改变

   点击html的startGoogleMap启动地图应用

阅读(741) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~