Kotlin AlertDialog按钮不运行lambda或存储在变量中的功能

发布于 2025-02-05 01:01:21 字数 1763 浏览 0 评论 0原文

我正在尝试创建一个有助于创建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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱冒险 2025-02-12 01:01:22

如果您需要不使用参数的lambda,则应将其定义为:

function1: ()->Unit

然后将其称为:

function1()

因此应该像这样:

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?() } 
    if (button2 != null) message.setNegativeButton(button2) { _, _ -> function2?() }
    message.show()

}

If you need a lambda that takes no parameter, it should be defined as

function1: ()->Unit

and then you call it as:

function1()

so it should be like this:

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?() } 
    if (button2 != null) message.setNegativeButton(button2) { _, _ -> function2?() }
    message.show()

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文