简体   繁体   English

Android Kotlin - 如何将 DialogInterface.OnClickListener 的默认值设置为 function 参数?

[英]Android Kotlin - How do i set a default value for DialogInterface.OnClickListener as function parameter?

This is my function declaration:这是我的 function 声明:

fun MyDialog(ctx: Context, msg: String, yestext: String = "", OnYes: DialogInterface.OnClickListener): AlertDialog

How do i set a default value for "OnYes: DialogInterface.OnClickListener"?如何为“OnYes:DialogInterface.OnClickListener”设置默认值?

I have tried OnYes: DialogInterface.OnClickListener = null but it doesn't work.我试过 OnYes:DialogInterface.OnClickListener = null 但它不起作用。

Answer provided by @mangkool @mangkool 提供的答案

OnYes: DialogInterface.OnClickListener? OnYes:DialogInterface.OnClickListener? = null = null

val builder = AlertDialog.Builder(this@MainActivity)

            // Set the alert dialog title
            builder.setTitle("App background color")

            // Display a message on alert dialog
            builder.setMessage("Are you want to set the app background color to RED?")

            // Set a positive button and its click listener on alert dialog
            builder.setPositiveButton("YES"){dialog, which ->
                // Do something when user press the positive button
                Toast.makeText(applicationContext,"Ok, we change the app background.",Toast.LENGTH_SHORT).show()

                // Change the app background color
                root_layout.setBackgroundColor(Color.RED)
            }


            // Display a negative button on alert dialog
            builder.setNegativeButton("No"){dialog,which ->
                Toast.makeText(applicationContext,"You are not agree.",Toast.LENGTH_SHORT).show()
            }


            // Display a neutral button on alert dialog
            builder.setNeutralButton("Cancel"){_,_ ->
                Toast.makeText(applicationContext,"You cancelled the dialog.",Toast.LENGTH_SHORT).show()
            }

            // Finally, make the alert dialog using builder
            val dialog: AlertDialog = builder.create()

            // Display the alert dialog on app interface
            dialog.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Kotlin - 如何将命名的 function 传递给另一个 ZC19C4252674C17A 中的 DialogInterface.OnClickListener 参数 - Android Kotlin - How to pass a named function to DialogInterface.OnClickListener parameter in another function? 如何在没有AlertDialog.Builder的情况下设置DialogInterface.OnClickListener? - How to set DialogInterface.OnClickListener without AlertDialog.Builder? Android:int在DialogInterface.OnClickListener()中为-1 - Android: int which in DialogInterface.OnClickListener() is -1 Android-从DialogInterface.OnClickListener引用main可运行 - Android - Refer to main runnable from DialogInterface.OnClickListener AppRater DialogInterface.OnClickListener询问视图 - AppRater DialogInterface.OnClickListener asking view 使用DialogInterface.OnClickListener区分单个对话框 - Differentiate single Dialogs with DialogInterface.OnClickListener 警报按钮的DialogInterface.OnClickListener错误 - DialogInterface.OnClickListener error for alert button 传递“ null”作为DialogInterface.OnClickLIstener合法吗? - Passing “null” as DialogInterface.OnClickLIstener legit? DialogInterface.OnClickListener中的startActivityForResult - startActivityForResult from within a DialogInterface.OnClickListener 方法调用-onActivityResult与DialogInterface.OnClickListener() - Method call - onActivityResult vs DialogInterface.OnClickListener()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM