Android Kotlin-如何将DialogInterface.OnClickListener的默认值设置为函数参数?
这是我的函数声明:Android Kotlin-如何将DialogInterface.OnClickListener的默认值设置为函数参数?,android,kotlin,dialog,onclicklistener,function-parameter,Android,Kotlin,Dialog,Onclicklistener,Function Parameter,这是我的函数声明: fun MyDialog(ctx: Context, msg: String, yestext: String = "", OnYes: DialogInterface.OnClickListener): AlertDialog 如何设置“OnYes:DialogInterface.OnClickListener”的默认值 我试过OnYes:DialogInterface.OnClickListener=null,但不起作用。答案由@mangkool提供 val build
fun MyDialog(ctx: Context, msg: String, yestext: String = "", OnYes: DialogInterface.OnClickListener): AlertDialog
如何设置“OnYes:DialogInterface.OnClickListener”的默认值
我试过OnYes:DialogInterface.OnClickListener=null,但不起作用。答案由@mangkool提供
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()
OnYes:DialogInterface.OnClickListener?=null是否要创建警报对话框?是,创建对话框是一个实用功能,该对话框应支持带有或不带有“是”按钮的对话框,因此需要为“是”按钮文本和OnClickListener提供可选参数。OnYes:DialogInterface.OnClickListener?=空,你忘了带问号让它为空哦是的,谢谢mangkool!你什么意思@Animeshsahuth这个答案和这个问题无关,或者是真的吗?在对这个问题的评论中,他说他想进行一次提醒对话,我给了他一个例子@AnimeshSahu