mvvm - 使用TextWatcher获取editText的旧值

标签 mvvm kotlin textwatcher

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

相关文章:

android - Kotlin Coroutines 等到完成 init block

java - TextChangedListener 在我身上崩溃了

android - Kotlin Android View 绑定(bind) : findViewById vs Butterknife vs Kotlin Android Extension

java - 使用 RadioButtons 实现 TextWatcher

java - 如何仅在使用android中的textwatcher从edittext完成编辑后才将值添加到hashmap

c# - 使用 MVVM 模式将项目添加到数据绑定(bind)列表框的最佳方法?

wpf - 'interactivity'和 'interactions'之间的WPF差异

c# - 寻找 mainviewmodel 上 MVVM 文本 block 绑定(bind)的指导

c# - 使用集合时如何在MVVM中同步数据

android - Unresolved reference 数据绑定(bind) - Android Studio 4.0