Kotlin 中带参数的 Lambda

冷静头脑

我想写TextWatcherEditText秒。在其中任何一个中,我都想为EditText变量赋值。像这样:

var variable: String? = null

private inner class CodeTextWatcher : TextWatcher {
    override fun afterTextChanged(s: Editable?) {
        variable = s?.toString()
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
    }
}

对于几个EditTexts 我想写一些类似的东西:

private inner class CodeTextWatcher(private val method: (String?) -> Unit) : TextWatcher {
    override fun afterTextChanged(s: Editable?) {
        method(s?.toString())
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
    }
}

并称之为:

textWatcher1 = CodeTextWatcher {variable1 = s}
textWatcher2 = CodeTextWatcher {variable2 = s}

但是我不能s在这里并且想safterTextChanged(s: Editable?). 是否有可能?

史诗熊猫力量

你有没有尝试过:

textWatcher1 = CodeTextWatcher { s -> variable1 = s }
textWatcher2 = CodeTextWatcher { s -> variable2 = s }

或者在这种情况下,甚至可能

textWatcher1 = CodeTextWatcher { variable1 = it }
textWatcher2 = CodeTextWatcher { variable2 = it }

?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章