I want to write TextWatcher for several EditTexts. In any of them I want to assign EditText value to a variable. Like this:
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) {
}
}
For several EditTexts I want to write something like:
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) {
}
}
and call it:
textWatcher1 = CodeTextWatcher {variable1 = s}
textWatcher2 = CodeTextWatcher {variable2 = s}
But I cannot write s here and want to access s from afterTextChanged(s: Editable?). Is it possible?