gpt4 book ai didi

java - 如何将值/变量从 android java 类传递到 android 4.4.2 kitkat 中的 javascript/.html 文件

转载 作者:行者123 更新时间:2023-11-29 22:03:34 26 4
gpt4 key购买 nike

我需要将 值/变量(例如整数、字符串) 从我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com