从在javascript中调用的Android函数中获取值,并将其返回给js
我已经尝试了这段代码并坚持了很多天。从在javascript中调用的Android函数中获取值,并将其返回给js,javascript,android,webview,Javascript,Android,Webview,我已经尝试了这段代码并坚持了很多天。 实际上,我想要的是集成mathjax库以获得mathametics公式的输出 <html> <head> <style> body{ background-color: #FA5858; color:#fff; } input{ background-color: #F7D358; width: 300px; padding:10px; color: #000; } div#content{ padding:20px;
实际上,我想要的是集成mathjax库以获得mathametics公式的输出
<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>
但我的android代码不会向javascript函数返回任何值。感谢您的帮助 像 这是我的android功能
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>
javascript中的logText函数在Android中调用Navive log方法
要从本机Android代码调用Javascript函数,必须执行以下操作:
String loadingUrl = "javascript:toBeCalledFromAndroid('" + text + "')";
webView.loadUrl(loadingUrl);
谢谢你的答复。。但我想传递字符串形式的WebAppInterface函数,并在javascript@PaMADo中获取其值。你误解了我的问题。你能告诉我如何将字符串形式的接口转换为javascript吗?看一看,我已经更新了我的问题..在您的示例中应该会显示一个祝酒词:String loadingUrl=“javascript:showAndroidToast(““+text+”)”);loadUrl(loadingUrl);这可以帮助你:谢谢。。我会检查的。。让你知道你是如何初始化你的网络视图的?请多发一点代码:)你好,我也有同样的问题,你解决了吗?
function logText(text) {
Android.log(text);
}
function toBeCalledFromAndroid(text) {
// Do something in javascript
}
String loadingUrl = "javascript:toBeCalledFromAndroid('" + text + "')";
webView.loadUrl(loadingUrl);