【发布时间】:2020-01-12 19:33:55
【问题描述】:
我已经使用 WebView 组件加载了一个网页并添加了一个 JavascriptInterface。请检查下面的代码,
val webview = WebView(this)
setContentView(webview)
webview.settings.javaScriptEnabled = true
webview.loadUrl(HOME_PAGE_URL)
webview.addJavascriptInterface(JavascriptInterface(),”javascript_bridge”)
当我使用 window.javascript_bridge.showToast(“Information Saved”); 从 Javascript 调用调用时
private inner class JavascriptInterface
{
@android.webkit.JavascriptInterface
fun showToast(text: String?)
{
Log.d("WEBVIEW", text);
}
}
我可以毫无问题地将方法从 Javascript 调用到 Kotlin。
但现在我想将一个对象从 Javascript 传递给 Kotlin,如下所示,
var info = {
message: “Information Saved”,
ID: 123456
}
当我使用window.javascript_bridge.showToast(info);从Javascript调用调用时
我尝试将数据类型更改为Any,但是从Javascript传递的值为null
private inner class JavascriptInterface
{
@android.webkit.JavascriptInterface
fun showToast(text: Any?)
{
Log.d("WEBVIEW", text.toString());
}
}
【问题讨论】:
标签: javascript android kotlin android-webview javascript-injection