来自:http://blog.csdn.net/android_tutor/article/details/5853143


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

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

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

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

[java] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="WelcometoMrWei'sBlog."
  11. />
  12. <WebView
  13. android:id="@+id/webview"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. />
  17. <Button
  18. android:id="@+id/button"
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:text="Changethewebviewcontent"
  22. />
  23. </LinearLayout>

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

[css] view plain copy
  1. <html>
  2. <mce:scriptlanguage="javascript"><!--
  3. functionfillContent(){
  4. document.getElementById("content").innerHTML=
  5. "ThisContentisshowedbyAndroidinvokeJavascriptfunction.";
  6. }
  7. //--></mce:script>
  8. <body>
  9. <p><aonClick="window.demo.startMap()"href="">StartGoogleMap</a></p>
  10. <pid="content"></p>
  11. <p>ADemo----AndroidandJavascriptinvokeeachother.</p>
  12. <p>Author:Frankiewei</p>
  13. </body>
  14. </html>

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

[java] view plain copy
  1. packagecom.tutor.webwiewdemo;
  2. importandroid.app.Activity;
  3. importandroid.content.ComponentName;
  4. importandroid.content.Intent;
  5. importandroid.os.Bundle;
  6. importandroid.view.View;
  7. importandroid.webkit.WebSettings;
  8. importandroid.webkit.WebView;
  9. importandroid.widget.Button;
  10. publicclassWebViewDemoextendsActivity{
  11. privateWebViewmWebView;
  12. privateButtonmButton;
  13. publicvoidonCreate(BundlesavedInstanceState){
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.main);
  16. setupViews();
  17. }
  18. //初始化
  19. privatevoidsetupViews(){
  20. mWebView=(WebView)findViewById(R.id.webview);
  21. WebSettingsmWebSettings=mWebView.getSettings();
  22. //加上这句话才能使用javascript方法
  23. mWebSettings.setJavaScriptEnabled(true);
  24. //增加接口方法,让html页面调用
  25. mWebView.addJavascriptInterface(newObject(){
  26. //这里我定义了一个打开地图应用的方法
  27. publicvoidstartMap(){
  28. IntentmIntent=newIntent();
  29. ComponentNamecomponent=newComponentName(
  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(newButton.OnClickListener(){
  41. publicvoidonClick(Viewv){
  42. mWebView.loadUrl("javascript:fillContent()");
  43. }
  44. });
  45. }
  46. }

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

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

点击html的startGoogleMap启动地图应用


更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. Python list sort方法的具体使用
  3. python list.sort()根据多个关键字排序的方法实现
  4. Android(安卓)触控事件解析 - Mastering The Android(安卓)Touch
  5. Android(安卓)控件使用之SlidingDrawer
  6. Android源码阅读分析:Activity生命周期
  7. Android设计模式系列--工厂方法模式
  8. [Android]实现静默安装APK的两种方法
  9. Wifi启动流程分析

随机推荐

  1. JavaScript 程序员可以从C ++中学到什么[
  2. centos7 OpenSSL1.1.1i rpm自动打包升级
  3. 用 TypeScript 开发 Node.js 程序[每日前
  4. MAC地址表和ARP表分别是什么?
  5. 6道tomcat面试题,最后两道难倒我了
  6. ESXi GPU 直通
  7. RPA:如何给财务人员带来新机遇?
  8. 快速上手最新的 Vue CLI 3[每日前端夜话0
  9. 浏览器中的JavaScript:文档对象模型与 DOM
  10. Android(安卓)init源代码分析(1)概要分析