我想使用beforeTextWatcher函数获取editText的旧值。
问题是,当我尝试获取此旧值时,返回的值始终为null,请有人帮帮我
val nomTextWatcher: TextWatcher
get() = object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {
userUpdateSignup.setNom(s.toString())
}
override fun onTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {
if(!s.toString().isEmpty())
userUpdateSignup.setNom(s.toString())
}
}
最佳答案
您不仅可以使用TextWatcher,还必须将值存储到类属性中,然后自己检查它。
private var myFieldValue : String = ""
[...]
val nomTextWatcher: TextWatcher
get() = object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {
userUpdateSignup.setNom(s.toString())
}
override fun onTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {
// Here you can check differences or what you want
if(!s.toString().isEmpty())
userUpdateSignup.setNom(s.toString())
// After text changed, you have to store it
myFieldValue = s.toString()
}
}
关于mvvm - 使用TextWatcher获取editText的旧值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55763473/