gpt4 book ai didi

mvvm - 使用TextWatcher获取editText的旧值

转载 作者:行者123 更新时间:2023-12-03 10:42:06 24 4
gpt4 key购买 nike

我想使用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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com