在Android中通过WebView控件,可以实现要加载的页面与Android方法相互调用,我们要实现WebView中的addJavascriptInterface方法,这样html才能调用android方法

第一步:新建一个Android工程,命名为WebViewDemo

第二步:修改main.xml布局文件,增加了一个WebView控件还有Button控件,代码如下:
    <?xml version="1.0" encoding="utf-8"?>      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"          android:orientation="vertical"          android:layout_width="fill_parent"          android:layout_height="fill_parent"          >          <TextView                android:layout_width="fill_parent"               android:layout_height="wrap_content"               android:text="Welcome to Mr Wei's Blog."              />          <WebView              android:id="@+id/webview"              android:layout_width="fill_parent"               android:layout_height="wrap_content"           />          <Button              android:id="@+id/button"              android:layout_width="fill_parent"              android:layout_height="wrap_content"              android:text="Change the webview content"          />      </LinearLayout>  


第三步:在assets目录下新建一个demo.html文件,代码如下
<html>      <mce:script language="javascript"><!--             function fillContent(){              document.getElementById("content").innerHTML =                    "This Content is showed by Android invoke Javascript function.";          }        // --></mce:script>      <body>      <a onClick="window.demo.startMap()" href="">Start GoogleMap</a>            A Demo ----Android and Javascript invoke each other.      Author:Frankiewei    </body>  </html>


第四步:修改主核心程序WebViewDemo.java,代码如下:
    package cn.caiwb.webwiewdemo;      import android.app.Activity;      import android.content.ComponentName;      import android.content.Intent;      import android.os.Bundle;      import android.view.View;      import android.webkit.WebSettings;      import android.webkit.WebView;      import android.widget.Button;      public class WebViewDemo extends Activity {          private WebView mWebView;          private Button mButton;          public void onCreate(Bundle savedInstanceState) {              super.onCreate(savedInstanceState);              setContentView(R.layout.main);              setupViews();          }          //初始化          private void setupViews() {              mWebView = (WebView) findViewById(R.id.webview);              WebSettings mWebSettings = mWebView.getSettings();              //加上这句话才能使用javascript方法              mWebSettings.setJavaScriptEnabled(true);              //增加接口方法,让html页面调用              mWebView.addJavascriptInterface(new Object() {                  //这里我定义了一个打开地图应用的方法                  public void startMap() {                      Intent mIntent = new Intent();                      ComponentName component = new ComponentName(                              "com.google.android.apps.maps",                              "com.google.android.maps.MapsActivity");                      mIntent.setComponent(component);                      startActivity(mIntent);                  }              }, "demo");              //加载页面              mWebView.loadUrl("file:///android_asset/demo.html");              mButton = (Button) findViewById(R.id.button);              //给button添加事件响应,执行JavaScript的fillContent()方法              mButton.setOnClickListener(new Button.OnClickListener() {                  public void onClick(View v) {                      mWebView.loadUrl("javascript:fillContent()");                  }              });          }      }  


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

更多相关文章

  1. 向模拟器发短信打电话的方法
  2. 系出名门Android(5) - 控件(View)之TextView, Button, ImageButt
  3. Android(安卓)远程调试 JNI 实现
  4. Android怎么让RadioButton图片居中显示
  5. Android(安卓)五种布局简单介绍
  6. android设置Activity背景色为透明的2种方法
  7. RelativeLayout
  8. android:maxLines和android:ellipsize同时使用导致显示异常
  9. android手机屏幕适配方法

随机推荐

  1. Android(安卓)App开发基础篇—数据存储(S
  2. Android应用程序启动过程源代码分析
  3. 服务器搭建快速入门——适用于Android应
  4. Android(安卓)HAL
  5. Android开发规范详解
  6. Android(安卓)Content Providers(二)——Co
  7. 【Android】Android(安卓)多个APK数据共
  8. 【Android】背景知识
  9. Android(安卓)应用程序签名、发布
  10. Android键盘系统