我需要将值/变量(例如整数、字符串)从我的java类传递到Android Assets 内的.html文件 。假设它是一个基于用户点击的整数值。我需要将 this 整数传递给 .html 文件(脚本),以确定要进行哪个 ajax 调用(通过 if-else 循环)。我到处搜索但找不到这个问题的答案。
我需要在 android 4.4.2 上实现这一点。提前致谢。
最佳答案
你见过这个http://developer.android.com/guide/webapps/webview.html ?您对将 JavaScript 代码绑定(bind)到 Android 代码部分感兴趣。首先声明接口(interface),然后实现将返回整数的方法。然后在您的 JS 代码中调用此方法来获取值。
编辑
使用 JS 的接口(interface):
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Get the value */
@JavascriptInterface
public int getValue() {
return value
}
}
将此界面添加到您的WebView
:
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
在你的 JS 代码中:
var value = Android.getValue();
关于java - 如何将值/变量从 android java 类传递到 android 4.4.2 kitkat 中的 javascript/.html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22222265/