[英]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.