简体   繁体   English

在kotlin中作为参数使用默认参数

[英]function as parameter in kotlin with default argument

i have a function who have another function as parameter, and i want to set default function to my function parameter. 我有一个函数,该函数具有另一个函数作为参数,并且我想将默认函数设置为我的函数参数。

here i have a function like this 在这里我有这样的功能

public fun showDialog(context: Context, title: String = "-", mode: String = "info", type: String = "info", onOk : (dialog: Dialog) -> Unit = { onClickOk(dialog)) }) {

val dialog = Dialog(context)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(false)
dialog.setContentView(R.layout.modal_dialog)
dialog.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

dialog.dialogText.text = title

if (mode.equals("dialog")) {
    dialog.dialogYesNo.visibility = View.VISIBLE
    dialog.optionOk.visibility = View.GONE
} else {
    dialog.dialogYesNo.visibility = View.GONE
    dialog.optionOk.visibility = View.VISIBLE
}

if (type.equals("success")) {
    dialog.optionOk.background = context.getDrawable(R.drawable.btn_rounded_success)
    dialog.dialogAlert.background = context.getDrawable(R.drawable.dialog_alert_success)
} else if (type.equals("fail")) {
    dialog.optionOk.background = context.getDrawable(R.drawable.btn_rounded_fail)
    dialog.dialogAlert.background = context.getDrawable(R.drawable.dialog_alert_fail)
} else {
    dialog.optionOk.background = context.getDrawable(R.drawable.btn_rounded_primary)
    dialog.dialogAlert.background = context.getDrawable(R.drawable.dialog_alert_info)
}

dialog.optionOk.setOnClickListener{
    onOk(dialog)
}

dialog.show()
}

and here my onClickOk function 这是我的onClickOk函数

public fun onClickOk (dialog: Dialog){
    dialog.hide()
}

my function as parameter is in this code : 我作为参数的功能在以下代码中:

onOk : (dialog: Dialog) -> Unit

and i give a default argument to my function parameter in this code 我在这段代码中给我的函数参数一个默认参数

onOk : (dialog: Dialog) -> Unit = { onClickOk(dialog)) }

but i can't passing the "dialog" parameter on onOk function to onClickOk function 但是我无法将onOk函数的“ dialog”参数传递给onClickOk函数

this is my error code screenshot 这是我的错误代码截图

在此处输入图片说明

can someone help me please ? 有人能帮助我吗 ? Thanks 谢谢

您可以传递方法参考:

onOk : (dialog: Dialog) -> Unit = this::onClickOk

Names in the function type notation are only for the documentation purposes and you won't be able to use them in the lambda's body: 函数类型符号中的名称仅出于文档目的,您将无法在lambda的正文中使用它们:

The function type notation can optionally include names for the function parameters: (x: Int, y: Int) -> Point. 函数类型符号可以选择包括函数参数的名称:(x:Int,y:Int)->点。 These names can be used for documenting the meaning of the parameters 这些名称可用于记录参数的含义

If your lambda has only one argument, you can use an implicit it parameter to refer to the argument: 如果您的lambda只有一个参数,则可以使用一个隐式的it参数来引用该参数:

onOk: (dialog: Dialog) -> Unit = { onClickOk(it) }

In case of two or more arguments you have to declare them inside the lambda's body: 如果有两个或多个参数,则必须在lambda的体内声明它们:

onOk: (Dialog, String) -> Unit = { dialog, title -> onClickOk(dialog, title) }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM