Android:WebView与Javascript交互(相互调用参数、传值) 新人不错

https://blog.csdn.net/books1958/article/details/44747045

Android:WebView与Javascript交互(相互调用参数、传值)

2015年03月30日 10:32:13

阅读数:20273

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

效果图:

(一)Android部分:

布局代码:

 
  1. <LinearLayout xmlns: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.  
  11. <LinearLayout

  12. android:layout_width="match_parent"

  13. android:layout_height="wrap_content">

  14.  
  15. <EditText

  16. android:id="@+id/input_et"

  17. android:layout_width="0dp"

  18. android:layout_height="wrap_content"

  19. android:singleLine="true"

  20. android:layout_weight="1"

  21. android:hint="请输入信息" />

  22.  
  23. <Button

  24. android:text="Java调用JS"

  25. android:layout_width="wrap_content"

  26. android:layout_height="wrap_content"

  27. android:onClick="sendInfoToJs" />

  28. </LinearLayout>

  29.  
  30. <WebView

  31. android:id="@+id/webView"

  32. android:layout_width="match_parent"

  33. android:layout_height="match_parent" />

  34.  
  35. </LinearLayout>

Activity代码:

 
  1. /**

  2. * Android WebView 与 Javascript 交互。

  3. */

  4. public class MainActivity extends ActionBarActivity {

  5. private WebView webView;

  6.  
  7. @SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"})

  8. @Override

  9. protected void onCreate(Bundle savedInstanceState) {

  10. super.onCreate(savedInstanceState);

  11. setContentView(R.layout.activity_main);

  12.  
  13. webView = (WebView) findViewById(R.id.webView);

  14.  
  15. webView.setVerticalScrollbarOverlay(true);

  16. //设置WebView支持JavaScript

  17. webView.getSettings().setJavaScriptEnabled(true);

  18.  
  19. String url = "http://192.168.1.27/js_17_android_webview.html";

  20. webView.loadUrl(url);

  21.  
  22. //在js中调用本地java方法

  23. webView.addJavascriptInterface(new JsInterface(this), "AndroidWebView");

  24.  
  25. //添加客户端支持

  26. webView.setWebChromeClient(new WebChromeClient());

  27. }

  28.  
  29. private class JsInterface {

  30. private Context mContext;

  31.  
  32. public JsInterface(Context context) {

  33. this.mContext = context;

  34. }

  35.  
  36. //在js中调用window.AndroidWebView.showInfoFromJs(name),便会触发此方法。

  37. @JavascriptInterface

  38. public void showInfoFromJs(String name) {

  39. Toast.makeText(mContext, name, Toast.LENGTH_SHORT).show();

  40. }

  41. }

  42.  
  43. //在java中调用js代码

  44. public void sendInfoToJs(View view) {

  45. String msg = ((EditText) findViewById(R.id.input_et)).getText().toString();

  46. //调用js中的函数:showInfoFromJava(msg)

  47. webView.loadUrl("javascript:showInfoFromJava('" + msg + "')");

  48. }

  49. }

(二)网页代码:

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  3.  
  4. <html>

  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  6. <meta http-equiv="Content-Language" content="zh-cn" />

  7.  
  8. <title>Android WebView 与 Javascript 交互</title>

  9. <head>

  10. <style>

  11. body {background-color:#e6e6e6}

  12.  
  13. .rect

  14. {

  15. color:white;

  16. font-family:Verdana, Arial, Helvetica, sans-serif;

  17. font-size:16px;

  18. width:100px;

  19. padding:6px;

  20. background-color:#98bf21;

  21. text-decoration:none;

  22. text-align:center;

  23. border:none;

  24. cursor:pointer;

  25. }

  26.  
  27. .inputStyle {font-size:16px;padding:6px}

  28. </style>

  29. </head>

  30.  
  31. <body>

  32. <p>测试Android WebView 与 Javascript 交互</p>

  33. <input id = "name_input" class = "inputStyle" type="text"/>

  34. <a class = "rect" onclick="sendInfoToJava()">JS调用Java</a>

  35.  
  36. <script>

  37. function sendInfoToJava(){

  38. //调用android程序中的方法,并传递参数

  39. var name = document.getElementById("name_input").value;

  40. window.AndroidWebView.showInfoFromJs(name);

  41. }

  42.  
  43. //在android代码中调用此方法

  44. function showInfoFromJava(msg){

  45. alert("来自客户端的信息:"+msg);

  46. }

  47. </script>

  48.  
  49. </body>

  50. </html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值