从javascript中调用的Android函数中获取值,并将其返回给js
get value form Android function that is call in javascript and return it to js
我已经尝试过这段代码,并在表单中停留了很多天
实际上,我想要的是为数学公式集成mathjax库,并从中获得输出
<html>
<head>
<style>
body{
background-color: #FA5858;
color:#fff;
}
input{
background-color: #F7D358;
width: 300px;
padding:10px;
color: #000;
}
div#content{
padding:20px;
background-color: #F7D358;
color: #000;
}
</style>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
function showAndroidToast(toastmsg) {
var x= Android.showToast(toastmsg);
return x;
}
function testcall(toastmsg){
alert(showAndroidToast(toastmsg));
}
</script>
</head>
<body>
<center>
<h3>Binding JavaScript code to Android code</h3>
<div id="content">
</div>
<div>
Here are few examples:
When '(a 'ne 0'), there are two solutions to '(ax^2 + bx + c = 0') and they are
$$x = {-b 'pm 'sqrt{b^2-4ac} 'over 2a}.$$
</div>
<div>
<input type="button" value="Make Toast" onClick="testcall('Toast made by Javascript New:)')" /><br/>
</div>
</center>
</body>
</html>
这在我的安卓代码中
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/**
* Show Toast Message
* @param toast
*/
public String showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
String test="''hspace1ex ''color{blue} {Simplify:} ''hspace1ex 2x(3x+2xy)";
return test;
}
}
但我的android代码并没有返回任何javascript函数
感谢您的帮助。
比如
这是我的安卓功能
public String showToast() {
String test="''hspace1ex ''color{blue} {Simplify:} ''hspace1ex 2x(3x+2xy)";
return test;
}
and in javascript i want the value that is return by this showToast() function
感谢
这是我在应用程序中所做的:
设置Webview:
final WebView webView = new WebView(context);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new WebViewInterface(), "Android");
webView.loadUrl("file:///android_asset/web/page.html");
使用此WebViewInterface实现:
import android.webkit.JavascriptInterface;
public class WebViewInterface {
@JavascriptInterface
public void log(String text) {
Log.d("TAG", text);
}
}
page.html位于assets/web文件夹中,如下所示:
<!DOCTYPE html>
<html>
<head>
<script language="javascript" src="script.js"> </script>
</head>
<body>
</body>
</html>
script.js文件也位于assets/web文件夹中:
function logText(text) {
Android.log(text);
}
function toBeCalledFromAndroid(text) {
// Do something in javascript
}
javascript中的logText函数调用Android中的Navive日志方法。
要从原生Android代码调用Javascript函数,您必须执行以下操作:
String loadingUrl = "javascript:toBeCalledFromAndroid('" + text + "')";
webView.loadUrl(loadingUrl);
相关文章:
- Angular js-返回一个包含类似
- 为什么我的get-requesttoserver.js返回离子云9上的实际文件
- Javascript clientHeight在devtools控制台中返回正确的值,但从js返回0
- instafeed.js返回的居中图像
- Node JS返回主机名
- Ember.js返回相同的模型
- 安卓webview活动,如何获取js返回值
- 当用户离线时使用角度 js 返回缓存响应
- 从 PHP-JS 返回数组的计算的读取值
- 从 Backbone 发送发布请求.js返回跨域错误
- 僵尸.js返回不正确的页面内容
- 节点.js 返回 301 重定向的请求
- 乘法后,js 返回数字和 NaN
- js/vendor/jquery.js 返回 404 错误
- MongoDB find() 使用 mongoose - node.js 返回空
- JS返回一个指向带有特定ID的页面的链接
- HTML JS返回错误“无法设置空值”
- node.js返回GMT时间,而不是“”的本地时间;新日期()”;.那是个虫子吗
- image使用JS返回值“null”
- 客户端JS:返回异步请求的串联