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

效果图:


(一)Android部分:

布局代码:

[html]  view plain copy
  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代码: [java]  view plain copy
  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. }  
(二)网页代码: [html]  view plain copy
  1. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2.   
  3. <html>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <meta http-equiv="Content-Language" content="zh-cn" />  
  6.   
  7. <title>Android WebView 与 Javascript 交互title>  
  8. <head>  
  9.   <style>  
  10.   body {background-color:#e6e6e6}  
  11.   
  12.   .rect  
  13.   {  
  14.     color:white;  
  15.     font-family:Verdana, Arial, Helvetica, sans-serif;  
  16.     font-size:16px;  
  17.     width:100px;  
  18.     padding:6px;  
  19.     background-color:#98bf21;  
  20.     text-decoration:none;  
  21.     text-align:center;  
  22.     border:none;  
  23.     cursor:pointer;  
  24.   }  
  25.   
  26.   .inputStyle {font-size:16px;padding:6px}  
  27.   style>  
  28. head>  
  29.   
  30. <body>  
  31.   <p>测试Android WebView 与 Javascript 交互p>  
  32.   <input id = "name_input" class = "inputStyle" type="text"/>  
  33.   <a class = "rect" onclick="sendInfoToJava()">JS调用Javaa>  
  34.   
  35.   <script>  
  36.   function sendInfoToJava(){  
  37.     //调用android程序中的方法,并传递参数  
  38.     var name = document.getElementById("name_input").value;  
  39.     window.AndroidWebView.showInfoFromJs(name);  
  40.   }  
  41.   
  42.   //在android代码中调用此方法  
  43.   function showInfoFromJava(msg){  
  44.     alert("来自客户端的信息:"+msg);  
  45.   }  
  46.   script>  
  47.   
  48. body>  
  49. html>  

更多相关文章

  1. android琐碎记四
  2. Android(安卓)Content Provider详解及示例代码
  3. Android如何在java代码中设置margin
  4. Android的IPC机制Binder的详解(转发)
  5. android Uri获取真实路径转换成File的方法
  6. android panellistview 圆角实现代码
  7. android:configChanges属性总结
  8. Android(安卓)8.0 Activity启动过程分析
  9. android 常用intent

随机推荐

  1. How to Run Android Applications on Ubu
  2. android 随手记 SQLITE代码 直接能用
  3. Android判断当前的Activity
  4. Drawable简单使用
  5. Android下如何卸载和格式化sdcard
  6. android 打电话
  7. android自定义keystore
  8. OpenGL ES教程VI之纹理贴图(原文对照)
  9. Android AGPS 定位 测试程序
  10. android 2.3 StrictMode 使用