【问题标题】:in setOnClickListeners (Kotlin), what is the different between { it } and { view: View -> view... }?在 setOnClickListener (Kotlin) 中,{ it } 和 { view: View -> view... } 有什么区别?
【发布时间】:2021-01-01 04:04:19
【问题描述】:

在 setOnClickListeners (Kotlin) 中,{ it } 和 { view: View -> view... } 有什么区别?
我都试过了,都有效。所以我想知道有什么不同?如果我总是使用 {it} 可以吗?因为我更容易记住。
->符号有什么用?

binding.playButton.setOnClickListener { view: View ->
            view.findNavController().navigate(R.id.action_titleFragment_to_gameFragment)
        }

binding.playButton.setOnClickListener {
            it.findNavController().navigate(R.id.action_titleFragment_to_gameFragment)
        }

提前致谢:)

【问题讨论】:

标签: android kotlin data-binding navigation fragment


【解决方案1】:

当 lambda 只有一个参数时,它默认显示为 it。您可以选择使用语法为它指定一个明确的名称:{ name -> }。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多