获取在 javascript 和 return 中调用到 js 的 Android 函数的值
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>
这在我的 android 代码中
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 代码 return 对 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
谢谢
这是我在应用程序中所做的:
设置网络视图:
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);
我已经尝试过此代码并卡住了很多天。
实际上我想要的是为数学公式集成 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>
这在我的 android 代码中
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 代码 return 对 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
谢谢
这是我在应用程序中所做的:
设置网络视图:
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);