【问题标题】:Get the old value of editText with TextWatcher使用 TextWatcher 获取 editText 的旧值
【发布时间】:2019-04-19 14:33:44
【问题描述】:

我想用 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())

            }
        }

【问题讨论】:

    标签: mvvm kotlin textwatcher


    【解决方案1】:

    您不能只使用 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()
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多