Android JAVA和JS的通信

//目录结构


//效果


//一起来看下代码

//index.html HTML页面

  1. <html>  
  2. <head>  
  3. <meta http-equiv="Content-Type" content="text/html; charset=GBK" />  
  4.     <script>  
  5.         function showInfo(str)  
  6.         {  
  7.             document.getElementById("info").innerHTML=str;  
  8.         }  
  9.     </script>  
  10. </head>  
  11.     <body>  
  12.         重Android传来的值是:<span id="info"> </span>  
  13.         <br>  
  14.         <a href="#" onclick="window.android.callAndroid('你好,Android')">  
  15.         call Android from javascript  
  16.         </a>  
  17.     </body>  
  18. </html>  
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
	<script>
		function showInfo(str)
		{
			document.getElementById("info").innerHTML=str;
		}
	</script>
</head>
	<body>
		重Android传来的值是:<span id="info"> </span>
		<br>
		<a href="#" οnclick="window.android.callAndroid('你好,Android')">
		call Android from javascript
		</a>
	</body>
</html>

//main.xml主布局

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns: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.       
  8.     <Button   
  9.         android:id="@+id/sendtojs"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_width="fill_parent"  
  12.         android:text="发送数据到JS"  
  13.         />  
  14.     <TextView   
  15.         android:id="@+id/fromjsdata"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_width="fill_parent"  
  18.         />  
  19.     <EditText   
  20.         android:id="@+id/sendText"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_width="fill_parent"  
  23.         />  
  24.     <WebView  
  25.         android:id="@+id/web"  
  26.         android:layout_width="fill_parent"  
  27.         android:layout_height="fill_parent"  
  28.           
  29.         />  
  30.       
  31. </LinearLayout>  
<?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"
    >
	
    <Button 
        android:id="@+id/sendtojs"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="发送数据到JS"
        />
    <TextView 
        android:id="@+id/fromjsdata"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        />
    <EditText 
        android:id="@+id/sendText"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        />
    <WebView
        android:id="@+id/web"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        
        />
    
</LinearLayout>

//JSJAVAActivity.java JAVA控制代码

  1. package sn.len.jsjava;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.webkit.WebView;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11. import android.widget.TextView;  
  12.   
  13. public class JSJAVAActivity extends Activity implements OnClickListener  
  14. {  
  15.       
  16.     private WebView web_webView;  
  17.     private Button but_sendToJs;  
  18.     private EditText edi_sendText;  
  19.     private TextView tex_fromjsdata;  
  20.     private Handler handler = new Handler();  
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState)  
  23.     {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.main);  
  26.           
  27.         web_webView=(WebView)findViewById(R.id.web);  
  28.         edi_sendText=(EditText)findViewById(R.id.sendText);  
  29.         but_sendToJs=(Button)findViewById(R.id.sendtojs);  
  30.         tex_fromjsdata=(TextView)findViewById(R.id.fromjsdata);  
  31.           
  32.         but_sendToJs.setOnClickListener(this);  
  33.           
  34.         //开启访问JS代码功能   
  35.         web_webView.getSettings().setJavaScriptEnabled(true);  
  36.         //根据地址加载网页   
  37.         //文件头: file://   
  38.         //文件路径: /android_asset/index.html   
  39.         web_webView.loadUrl("file:///android_asset/index.html");  
  40.           
  41.         //参数1,把new SendToJs()这个对象构建到WEB浏览器中   
  42.         //参数2,提供在WEB浏览器中好让javascript访问的对象   
  43.         //例如: 在WEB中,javascript调用callAndroid(value)方法写法为window.android.callAndroid('value');   
  44.         web_webView.addJavascriptInterface(new SendToJs(), "android");  
  45.     }  
  46.     @Override  
  47.     public void onClick(View v)  
  48.     {  
  49.         //向浏览器发送数据,调用JS的showInfo函数   
  50.         web_webView.loadUrl("javascript:showInfo('"+edi_sendText.getText().toString()+"')");  
  51.     }  
  52.     class SendToJs  
  53.     {  
  54.         public void callAndroid(final String arg)  
  55.         {  
  56.             //利用Handler更新UI   
  57.             handler.post  
  58.             (  
  59.                     new Runnable()  
  60.                     {  
  61.                         @Override  
  62.                         public void run()  
  63.                         {  
  64.                             //设置重JS发过来的数据   
  65.                             tex_fromjsdata.setText("重javascript发来的数据是:"+arg);  
  66.                         }  
  67.                     }  
  68.             );  
  69.         }  
  70.     }  
  71. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值