Android中可以使用WebView加载网页,同时Android端的java代码可以与网页上的javascript代码之间相互调用。

效果图:


(一)Android部分:

布局代码:

[html] view plain copy
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:focusable="true"
  6. android:focusableInTouchMode="true"
  7. android:orientation="vertical"
  8. android:padding="8dp"
  9. tools:context=".MainActivity">
  10. <LinearLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content">
  13. <EditText
  14. android:id="@+id/input_et"
  15. android:layout_width="0dp"
  16. android:layout_height="wrap_content"
  17. android:singleLine="true"
  18. android:layout_weight="1"
  19. android:hint="请输入信息"/>
  20. <Button
  21. android:text="Java调用JS"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:onClick="sendInfoToJs"/>
  25. </LinearLayout>
  26. <WebView
  27. android:id="@+id/webView"
  28. android:layout_width="match_parent"
  29. android:layout_height="match_parent"/>
  30. </LinearLayout>
Activity代码: [java] view plain copy
  1. /**
  2. *AndroidWebView与Javascript交互。
  3. */
  4. publicclassMainActivityextendsActionBarActivity{
  5. privateWebViewwebView;
  6. @SuppressLint({"SetJavaScriptEnabled","AddJavascriptInterface"})
  7. @Override
  8. protectedvoidonCreate(BundlesavedInstanceState){
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. webView=(WebView)findViewById(R.id.webView);
  12. webView.setVerticalScrollbarOverlay(true);
  13. //设置WebView支持JavaScript
  14. webView.getSettings().setJavaScriptEnabled(true);
  15. Stringurl="http://192.168.1.27/js_17_android_webview.html";
  16. webView.loadUrl(url);
  17. //在js中调用本地java方法
  18. webView.addJavascriptInterface(newJsInterface(this),"AndroidWebView"); //重要的码
  19. //添加客户端支持
  20. webView.setWebChromeClient(newWebChromeClient());
  21. }
  22. privateclassJsInterface{
  23. privateContextmContext;
  24. publicJsInterface(Contextcontext){
  25. this.mContext=context;
  26. }
  27. //在js中调用window.AndroidWebView.showInfoFromJs(name),便会触发此方法。 ***************
  28. @JavascriptInterface
  29. publicvoidshowInfoFromJs(Stringname){
  30. Toast.makeText(mContext,name,Toast.LENGTH_SHORT).show();
  31. }
  32. }
  33. //在java中调用js代码
  34. publicvoidsendInfoToJs(Viewview){
  35. Stringmsg=((EditText)findViewById(R.id.input_et)).getText().toString();
  36. //调用js中的函数:showInfoFromJava(msg)
  37. webView.loadUrl("javascript:showInfoFromJava('"+msg+"')");
  38. }
  39. }
(二)网页代码: [html] view plain copy
  1. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html>
  4. <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
  5. <metahttp-equiv="Content-Language"content="zh-cn"/>
  6. <title>AndroidWebView与Javascript交互</title>
  7. <head>
  8. <style>
  9. body{background-color:#e6e6e6}
  10. .rect
  11. {
  12. color:white;
  13. font-family:Verdana,Arial,Helvetica,sans-serif;
  14. font-size:16px;
  15. width:100px;
  16. padding:6px;
  17. background-color:#98bf21;
  18. text-decoration:none;
  19. text-align:center;
  20. border:none;
  21. cursor:pointer;
  22. }
  23. .inputStyle{font-size:16px;padding:6px}
  24. </style>
  25. </head>
  26. <body>
  27. <p>测试AndroidWebView与Javascript交互</p>
  28. <inputid="name_input"class="inputStyle"type="text"/>
  29. <aclass="rect"onclick="sendInfoToJava()">JS调用Java</a>
  30. <script>
  31. functionsendInfoToJava(){
  32. //调用android程序中的方法,并传递参数
  33. varname=document.getElementById("name_input").value;
  34. window.AndroidWebView.showInfoFromJs(name);
  35. }
  36. //在android代码中调用此方法
  37. functionshowInfoFromJava(msg){
  38. alert("来自客户端的信息:"+msg);
  39. }
  40. </script>
  41. </body>
  42. </html>

更多相关文章

  1. Android(安卓)'记住密码'功能
  2. 【边做项目边学Android】手机安全卫士05_1:程序主界面
  3. android arm debug
  4. View常见XML属性及相关方法
  5. Android中字体加粗
  6. JS与Android交互之html页面跳转到Android(安卓)Activity
  7. android中wifi原理及流程分析
  8. 转:Android(安卓)开发技巧杂集
  9. Android多媒体应用——ImageSwitcher

随机推荐

  1. Android学习笔记(三):Andriod程序框架
  2. Android四大组件之Activity(一)
  3. Tween动画介绍
  4. android中设置控件边框以及如何保留上或
  5. Android(安卓)APP自动更新
  6. android Textview加下划线
  7. Gradle for Android(安卓)第四篇( 构建变
  8. Android使用WebView加载网页及数据__2020
  9. 2017年Android开源项目及库汇总
  10. Android(安卓)显示系统分析