Android WebView与javaScript之间的交互
2016-07-13 14:50
435 查看
学习了下,webview js之间的交互,项目中马上用到。JS调用java代码效果图 java代码调用javasrcipt代码效果图index.html代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd";> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript" language="javascript"> var share = JSON.stringify({"title": "sinodata", "desc": "ios", "shareUrl": "http://www.sinodata.com.cn" }); function sendInfoToJava(){ window.AndroidWebView.showInfoFromJs(share); } <!--在android代码中调用此方法--> function showInfoFromJava(msg){ alert("showInfoFromJava:"+msg); } </script> </head> <body la> <div id='b'> <input onclick="sendInfoToJava()" type="button" value="sendInfoToJava"/> </div> </body> </html>布局代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.chenjifang.webview.MainActivity"> <Button android:id="@+id/test_btn" android:text="代码中调用web js代码传递参数" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/test_edt" android:layout_width="match_parent" android:layout_height="wrap_content" /> <WebView android:id="@+id/test_webview" android:layout_width="match_parent" android:layout_height="400dp"></WebView> </LinearLayout>java代码:
public class MainActivity extends AppCompatActivity { private WebView mWebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebView = (WebView) findViewById(R.id.test_webview); //设置WebView支持JavaScript mWebView.getSettings().setJavaScriptEnabled(true); mWebView.loadUrl("file:///android_asset/index.html"); mWebView.addJavascriptInterface(new JsInterface(this), "AndroidWebView"); //添加客户端支持 mWebView.setWebChromeClient(new WebChromeClient()); findViewById(R.id.test_btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendInfoToJs(); } }); } private class JsInterface { private Context mContext; public JsInterface(Context context) { this.mContext = context; } //在js中调用window.AndroidWebView.showInfoFromJs(name),便会触发此方法。 @JavascriptInterface public void showInfoFromJs(String share) { Toast.makeText(mContext, share, Toast.LENGTH_SHORT).show(); } } //在java中调用js代码 public void sendInfoToJs() { String msg = ((EditText)findViewById(R.id.test_edt)).getText().toString(); //调用js中的函数:showInfoFromJava(msg) mWebView.loadUrl("javascript:showInfoFromJava('" + msg + "')"); }总结下,java代码中要设置webview对javascript的支持,
addJavascriptInterface(new JsInterface(this), "AndroidWebView");//这句代码中的第二个参数是在js访问方法的地址。
window.AndroidWebView.showInfoFromJs(share);
相关文章推荐
- Android 热修复 - AndFix 使用心得
- 验证出错,错误码:230,请AndroidManifest.xml文件中检查 kye 设置。
- 谈谈Android 6.0运行时权限理解
- Android 自定义Dialog 实例
- android中的fragment
- android动画基础--旋转移动平移缩放
- Android基础知识--Preference的使用
- Android MediaPlayer 类图和调用关系
- 获取Android APK JNI库
- 4000 推荐几款实用的Android Studio 插件
- Android注解神器ButterKnife使用说明
- Android使用Glide加载Gif.解决Glide加载Gif非常慢问题
- android搜索热词(热门标签)流式布局的实现
- 安卓学习笔记---Android颜色代码表
- textview段落首字母缩进俩个字符
- 70.Android开发知识点总结
- Android版本和API Level对应关系
- Android4.0.3/Android4.2.2 添加 Ethernet
- Android - 文件读写操作 总结
- Android跑马灯两种方式