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

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

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

第二步:修改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文件,代码如下(这里不知道为何多了mce:这几个东东,<script></script>这样是对的):

<html>
<mce:script language="javascript"><!--

function fillContent(){
   document.getElementById("content").innerHTML =
        "This Content is showed by Android invoke Javascript function.";
  }

// --></mce:script> 
  <body>
<p><a onClick="window.demo.startMap()" href="">Start GoogleMap</a></p>
<p id="content"></p>
<p>A Demo ----Android and Javascript invoke each other.</p>
<p>Author:Frankiewei</p>
  </body>
</html>

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

package com.tutor.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()");
   }
  });
}
}

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

    

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

点击html的startGoogleMap启动地图应用

Android中通过WebView控件实现与JavaScript方法相互调用的地图应用的更多相关文章

  1. android中的EditView控件

    android中的EditView控件 EditText继承关系:View-->TextView-->EditText ,EditText是可编辑文本框 1.EditText默认情况下,光 ...

  2. C#中的BackgroundWorker控件+Delegate.Invoke (委托同步调用)

    C#中的BackgroundWorker控件+Delegate.Invoke (委托同步调用) 简单代码,记录一下.一个BackgroundWorker控件  backgroundWorkerRefr ...

  3. 安卓开发学习笔记(五):史上最简单且华丽地实现Android Stutio当中Webview控件https/http协议的方法

    一.我们先在XML当中自定义一个webview(Second_layout.xml) 代码如下: <?xml version="1.0" encoding="utf ...

  4. android中的TextView控件

    我以前是搞ssh开发的,现在在搞android开发,所以简单学习了一下,对于自己所了解的做一个记录,也算是一个笔记吧,如果有什么不对的,希望大家给予一定的指导.  一.TextView的基本使用 Te ...

  5. Android中自定义IP控件

    最近在搞Android项目,之前并没有系统的去学过这方面的编程,只能边看书边撸代码.在项目的开发的过程中,需要一个IP控件,后面了解到Android中并没有这样的控件,于是网上搜索,发现得到的结果并不 ...

  6. android中去掉ListView控件中的分割线

    通过设置android:divider="@null" ,可以去掉ListView控件中的分割线 也可以自定义分割线的颜色,比如: <ListView android:id= ...

  7. Android中自定义组合控件

    Android中自定义控件的情况非常多,一般自定义控件可以分为两种:继承控件及组合控件.前者是通过继承View或其子类,重写方法实现自定义的显示及事件处理方式:后者是通过组合已有的控件,来实现结构的简 ...

  8. Android中动态改变控件的大小的一种方法

    在Android中有时候我们需要动态改变控件的大小.有几种办法可以实现  一是在onMeasure中修改尺寸,二是在onLayout中修改位置和尺寸.这个是可以进行位置修改的,onMeasure不行. ...

  9. Android中的常用控件之进度条(ProgressBar)

    ProgressBar的常用属性:style,进度条的样式,默认为圆形,用style="?android:attr/progressBarStyleHorizontal"可以将进度 ...

随机推荐

  1. WebForm+Web.config: 超时时间已到。在操作完成之前超时时间已过或服务器未响应。

    ylbtech-Error-WebForm+Web.config: 超时时间已到.在操作完成之前超时时间已过或服务器未响应. 超时时间已到.在操作完成之前超时时间已过或服务器未响应. 1.A,错误代码 ...

  2. TabWidget/TabHost的两种使用方法

    1.概念 盛放Tab的容器就是TabHost.TabHost的实现有两种方式: 第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab ...

  3. Android UI详解之Fragment加载

    使用Fragment的原因: 1. Activity间的切换不流畅 2. 模块化Activity,方便做局部动画(有时为了到达这一点要把多个布局放到一个activity里面,现在可以用多Fragmen ...

  4. 【LeetCode 169】Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  5. 软件测试技术(二)——使用等价类划分的方法进行的UI测试

    测试的目标程序 程序代码 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; impo ...

  6. HTML5的manifest缓存

    要使用manifest缓存,我们首先需要写一个manifest文件.这个文件有严格的格式要求,下面是个例子CACHE MANIFEST#我是注释,这个文件名叫test.manifestCACHE:/t ...

  7. CATALOGUE 目录

    1 语言基础 1.1 c/c++ [转]C/C++ 存储类型 作用域 连接类型 [转]C/C++内存划分 [转]C/C++除法实现方式及负数取模详解 [转]为什么C++编译器不能支持对模板的分离式编译 ...

  8. 网站繁简切换的JS遇到的一个BUG

    公司打算进入台湾市场,最近开发了繁体版本的网站,数据库里的信息全是简体,除了网页上固定的文字手动翻译了,文章内容标题都不是繁体. 于是在网上找了一段比较流行的繁简切换的JS实现了,不过后来却发现,有些 ...

  9. 踩刹车——regularization

    从一个问题说起: 当我们使用H10去拟合曲线的时候,其实我们只想要H2的结果.如果从H10变回到H2呢? 所以我们只需要添加上限制条件:w3=...=w10=0即可.现在呢,我们可以放宽一点条件:任意 ...

  10. 坚持自学的第二天,bootstrap初入门

    前言 昨天,初步学完了jekyll目录结构与Liquid语法的应用与认识. 日志 今天刚入门,做了一个bootstrap导航栏,但是选中状态不行,找了JS中写好的API,写法与视频中讲的有点不一样,但 ...