TextView addTextChangedListener Kotlin扩展函数

fun TextView.addTextWatcher(
        beforeTextChanged: ((CharSequence?, Int, Int, Int) -> Unit)? = null,
        onTextChanged: ((CharSequence?, Int, Int, Int) -> Unit)? = null,
        afterTextChanged: ((Editable) -> Unit)? = null,
) {

    addTextChangedListener(object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            beforeTextChanged?.invoke(s, start, count, after)
        }

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

        override fun afterTextChanged(s: Editable) {
            afterTextChanged?.invoke(s)
        }

    })

}

使用

textView.addTextWatcher(afterTextChanged = { s ->

    
})

你可能感兴趣的:(kotlin,android,textview)