![](/img/trans.png)
[英]Android Kotlin - How to pass a named function to DialogInterface.OnClickListener parameter in another function?
[英]Android Kotlin - How do i set a default value for DialogInterface.OnClickListener as function parameter?
這是我的 function 聲明:
fun MyDialog(ctx: Context, msg: String, yestext: String = "", OnYes: DialogInterface.OnClickListener): AlertDialog
如何為“OnYes:DialogInterface.OnClickListener”設置默認值?
我試過 OnYes:DialogInterface.OnClickListener = null 但它不起作用。
@mangkool 提供的答案
OnYes:DialogInterface.OnClickListener? = 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.