混合APP开发的套路(三): android和javascript初步交互(js调安卓)
2017-04-01 15:51
519 查看
http://blog.csdn.net/github_26672553/article/details/68944631
前面我们实现了,在安卓里调用 js代码。
现在我们来看看,js里如何调用android里的方法。
首先,我们在Activitity里(我们这是WebViewActivity.java),定义一个方法
定义好这个方法之后,还需要其他设置才行
2、网页部分
点击”click me”按钮就会调用
这个js函数中调用了android里定义的
注意看明白:代码里的
前面我们实现了,在安卓里调用 js代码。
现在我们来看看,js里如何调用android里的方法。
首先,我们在Activitity里(我们这是WebViewActivity.java),定义一个方法
@JavascriptInterface public void setText(final String txt){ // 在另一个线程处理 runOnUiThread(new Runnable() { @Override public void run() { TextView textView = (TextView)findViewById(R.id.wvText); textView.setText(txt); } }); }
定义好这个方法之后,还需要其他设置才行
// 给webView添加一个js接口(本类的,名字叫abc的) webView.addJavascriptInterface(this,"abc");
2、网页部分
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>this is title</title> <script> function show(){ document.getElementById("txtMsg").value = "hello javascript"; } function setAndroidText(){ var getValue = document.getElementById("txtMsg").value; window.abc.setText(getValue); } </script> </head> <body> <input type="text" id="txtMsg"> <button onclick="setAndroidText()"> click me</button> </body> </html>
点击”click me”按钮就会调用
setAndroidText()这个js函数,
这个js函数中调用了android里定义的
setText()方法。
注意看明白:代码里的
abc,这个我们在Android里定义了,在js是如何使用的。
相关文章推荐
- 混合APP开发的套路(三): android和javascript初步交互
- Android混合开发之WebViewJavascriptBridge实现JS与java安全交互
- android native+h5——混合开发初步,通过js交互数据
- Android混合开发之WebViewJavascriptBridge实现JS与java安全交互
- Android混合开发之WebViewJavascriptBridge实现JS与java安全交互
- Android混合开发之WebViewJavascriptBridge实现JS与java安全交互
- Android app混合开发,本地应用与htm5交互
- JavaScript与Android的数据交互,js与安卓数据交互WebView
- 混合app开发--js和webview之间的交互总结
- 安卓混合开发实例——JS与Java的交互
- Android混合开发之WebView与Javascript交互
- Android混合开发之WebView与Javascript交互
- h5混合开发之Js和android简单交互
- Android混合开发之WebView与Javascript交互
- android web混合开发 混合开发实例——JS与Java的交互
- Android混合开发之WebView与Javascript交互
- 混合APP开发的套路(6): Android发送http网络请求
- Android混合开发之WebView与Javascript交互
- 浅谈混合开发与Android,JS数据交互
- Android混合开发js与native交互两种方式