修改匿名函数以调用Kotlin中的另一个匿名函数 [英] Modifying an anonymous function to invoke another anonymous function in Kotlin
本文介绍了修改匿名函数以调用Kotlin中的另一个匿名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码中的点击监听器很少,如下所示:
I have few click listeners in my code like below:
tvLogin.setOnClickListener {
hideKeyBoard(it)
login()
}
tvForgotPassword.setOnClickListener {
hideKeyBoard(it)
navigateToForgetPassword()
}
我想修改传递的代码块,以始终先调用hideKeyBoard(view)
,然后再调用我的函数.
I want to modify the passed block of code to always invoke hideKeyBoard(view)
and then my function.
有没有一种方法可以创建一个高阶函数来修改代码块并调用传递的函数?
Is there a way to create a higher order function that would modify a block of code and invoke passed function?
我尝试了以下操作:
val clickListener: (View,()->Unit) -> Unit
但不确定如何运行.
任何人都可以帮助实现目标吗?
Can anyone please help how to achieve?
推荐答案
我不确定我是否完全理解您的意思,但也许是这样的:
I'm not sure I understand fully what you mean, but maybe something like this:
fun addCallTo(lambda: (View) -> Unit): (View) -> Unit {
return {
hideKeyboard(it)
lambda(it)
}
}
// usage:
tvLogin.setOnClickListener(addCallTo { view -> login() })
这篇关于修改匿名函数以调用Kotlin中的另一个匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文