android和js互调
来源:互联网 发布:mac锐捷客户端已损坏 编辑:程序博客网 时间:2024/05/17 02:40
第一步:
assets下新建web.html文件:
<html><head><meta http-equiv = "Content-Type" content="text/html;charset=UTF-8"><script type="text/javascript">function javacalljs(){document.getElementById("content").innerHTML=("<br\>JAVA调用了JS的无参函数"); } function javacalljswith(arg){document.getElementById("content").innerHTML=("<br\>"+arg); } </script></head><body>HTML内容显示<br/><h1><div id="content">内容显示</div></h1><br/><input type="button" value="点击调用java代码" onclick="window.android.startFunction()"/><br/><input type="button" value="点击调用java代码并传递参数" onclick="window.android.startFunction('http://blog.csdn.net/Leejizhou')"/></body></html>
第二步 android 代码
布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.chinafeisite.tianbu.a905test.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Android" /> <Button android:id="@+id/button" android:layout_margin="8dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="调用JS" android:background="@color/colorAccent" android:textColor="#ffffff" /> <Button android:id="@+id/button2" android:layout_margin="8dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="调用有参JS" android:background="@color/colorAccent" android:textColor="#ffffff" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="WebView" android:layout_marginTop="8dp" /> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>
第三步activity代码
contentWebView = (WebView) findViewById(R.id.webview); // 启用javascript contentWebView.getSettings().setJavaScriptEnabled(true); // 从assets目录下面的加载html contentWebView.loadUrl("file:///android_asset/web.html"); contentWebView.addJavascriptInterface(MainActivity.this,"android"); //Button按钮 无参调用HTML js方法 findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 无参数调用 JS的方法 contentWebView.loadUrl("javascript:javacalljs()"); } }); //Button按钮 有参调用HTML js方法 findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 传递参数调用JS的方法 contentWebView.loadUrl("javascript:javacalljswith(" + "'http://blog.csdn.net/Leejizhou'" + ")"); } }); } //由于安全原因 targetSdkVersion>=17需要加 @JavascriptInterface //JS调用Android JAVA方法名和HTML中的按钮 onclick后的别名后面的名字对应 @JavascriptInterface public void startFunction(){ runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,"show",3000).show(); } }); } @JavascriptInterface public void startFunction(final String text){ runOnUiThread(new Runnable() { @Override public void run() { new AlertDialog.Builder(MainActivity.this).setMessage(text).show(); } });
不要忘了网络权限
阅读全文
0 0
- android和js互调
- Android和js互调
- Android WebView和html中js互调
- android webview中java和js互调
- Android开发之webview和 js 互调
- Android和js通信
- Android和JS交互
- Android和Js交互
- Android和JS交互
- android和js交互
- Android和js交互
- android调用js和js调用android
- android webview 和 js交互
- Android 和 js 互相调用
- android和js相互调用
- Android WebView和JS交互
- Android和JS通信方案
- webview 和android js 交互
- 时钟同步提示:ntpdate[21931]: no server suitable for synchronization found
- UVA 729
- 日常学习总结
- 基于最少使用频次的LRU,LFU缓存淘汰算法
- TLS/SSL 协议详解 (2) SSL有关的密码学原理
- android和js互调
- android源码在线查看
- TCP/IP协议族(二) HTTP报文头解析
- Nordic 51822 烧录bootloader 后,需要OTA一次,才能启动App解决办法
- SQL CONCAT函数
- PostgreSQL新手入门
- UVA 10014
- [NOIP2011] 观光公交解题报告
- Hadoop MapReduce中的Partitioner