【发布时间】:2019-05-26 08:13:13
【问题描述】:
我正在使用 kotlin 开发具有 webview 和 webview javascript 的一些功能的本机应用程序。
用户可以点击页面上的按钮,然后调用本机 toast 或警报对话框
我完成了功能,它运行良好,现在我想将callback function添加到对话框中,当用户单击确定按钮(积极按钮)时,它将在网页上运行javascript,
网页上的Javascript
Android.showAlert("Title","Message",function(){alert(123)});
Javascript接口
@JavascriptInterface
fun showAlert(title:String,msg: String, callBack: Any?) {
val alertDialog = AlertDialog.Builder(mContext)
alertDialog.setTitle(title).setMessage(msg)
alertDialog.setPositiveButton("OK") { _, _->callBack())}
alertDialog.show()
Log.d('check',callBack) // Logged callback is null
}
代码似乎无法接收回调函数,如何解决并运行回调?
谢谢
【问题讨论】:
-
这个
JSPromise也可能有助于解决您的问题。
标签: javascript android kotlin webview