Kotlin AlertDialog按钮不运行lambda或存储在变量中的功能
我正在尝试创建一个有助于创建AlertDialog消息的新类。该类由收集所有参数(标题,内容/文本,按钮)的函数组成,并自动显示弹出窗口。
fun createPopupMessage(
context: Context, // get aplication context
title: String? = null, // get desired title as parameter
content: String? = null, // get content as a text
button1: String? = null, // define button1 text
function1: Unit? = null, // define button1 command (get some function)
button2: String? = null, // define button2 text
function2: Unit? = null // define button2 command (get some function)
) {
val message = AlertDialog.Builder(context)
if (title != null) message.setTitle(title)
if (content != null) message.setMessage(content)
if (button1 != null) message.setPositiveButton(button1) {_, _ -> function1} // ALERT: The expression "function1" is unused
if (button2 != null) message.setNegativeButton(button2) {_, _ -> kotlin.run { function2 }} // ALERT: The expression "function2" is unused too even whith kotlin.run command.
message.show()
}
问题在于,当我尝试将函数存储在函数1 或 function2 参数中时,按下按钮时不会执行该函数,但是当 creatsepopopupupmessage( )是创建的。
// running application
createPopupMessage(
context = this,
title = "Olá, mundo!",
content = "Testando um novo comando ^-^",
button1 = "Ok",
function1 = myFunction() // myFunction is called and executed here :/
)
// continue application
如何将myfunction()存储在 function1 变量中并仅在按下按钮时运行它?
注意:我尝试使用 function1 = {_-> myfunction} 作为lambda,但也行不通。 (((单位) - > unit)?或((任何) - > uniT)??
我尝试将函数1类型更改为 帮助:d
I'm trying to create a new class that helps create AlertDialog messages. This class consists of a function that collects all parameters (title, content/text, buttons) and shows the popup automaically.
fun createPopupMessage(
context: Context, // get aplication context
title: String? = null, // get desired title as parameter
content: String? = null, // get content as a text
button1: String? = null, // define button1 text
function1: Unit? = null, // define button1 command (get some function)
button2: String? = null, // define button2 text
function2: Unit? = null // define button2 command (get some function)
) {
val message = AlertDialog.Builder(context)
if (title != null) message.setTitle(title)
if (content != null) message.setMessage(content)
if (button1 != null) message.setPositiveButton(button1) {_, _ -> function1} // ALERT: The expression "function1" is unused
if (button2 != null) message.setNegativeButton(button2) {_, _ -> kotlin.run { function2 }} // ALERT: The expression "function2" is unused too even whith kotlin.run command.
message.show()
}
The problem is that when I try to store a function in the function1 or function2 parameters, the function is not executed when the button is pressed, but when createPopupMessage() is created.
// running application
createPopupMessage(
context = this,
title = "Olá, mundo!",
content = "Testando um novo comando ^-^",
button1 = "Ok",
function1 = myFunction() // myFunction is called and executed here :/
)
// continue application
How can I store myFunction() in function1 variable and run it only when the button is pressed?
Note: I tryed to use function1 = { _ -> myFunction } as a lambda, but it doesn't work too. I tryed change the function1 type to ((Unit) -> Unit)? or ((Any) -> Unit)? but happens the same :/
Thanks for help :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要不使用参数的lambda,则应将其定义为:
然后将其称为:
因此应该像这样:
}
If you need a lambda that takes no parameter, it should be defined as
and then you call it as:
so it should be like this:
}