【问题标题】:Android - Overriding onTextChanged doesn't change behaviorAndroid - 覆盖 onTextChanged 不会改变行为
【发布时间】:2020-09-02 16:52:21
【问题描述】:

我正在尝试创建一个自定义 EditText,它将在执行 onTextChanged 之前实现延迟。

class CustomEditText(context: Context, attributeSet: AttributeSet) : AppCompatEditText(context, attributeSet) {

     private var millisDelay: Long = 500

     private var timer: Timer? = null

     override fun onTextChanged(
         text: CharSequence?,
         start: Int,
         lengthBefore: Int,
         lengthAfter: Int
     ) {
         doDelay {
             Log.d("somekoder", "Calling onTextChanged after $millisDelay milliseconds")
             super.onTextChanged(text, start, lengthBefore, lengthAfter)
         }
     }

     fun setDelay(millisDelay: Long){
         this.millisDelay = millisDelay
     }

     private fun doDelay(then: () -> Unit){
         timer?.cancel()
         timer = Timer()
         // Log.d("somekoder", "Got action. Waiting $millisDelay milliseconds.")
         timer?.schedule(timerTask {
             then.invoke()
         }, millisDelay)
     }
}

在我的 MainActivity 中

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        search.setDelay(1000)

        search.addTextChangedListener {
            Log.d("somekoder", "MainActivity: onTextChanged")
        }
    }
}

这是我的日志的样子:

D/somekoder: MainActivity: onTextChanged
D/somekoder: MainActivity: onTextChanged
D/somekoder: MainActivity: onTextChanged
D/somekoder: MainActivity: onTextChanged
D/somekoder: Calling onTextChanged after 1000 milliseconds

MainActivity onTextChanged 被调用,即使我在那里有延迟。 有人可以解释我做错了什么吗? 提前致谢!

【问题讨论】:

    标签: android text view


    【解决方案1】:

    使用 addTextChangedListener() 您可以添加多个侦听器,不像 setOnclickListener 那样一次只能设置一个侦听器。作为一般的 api 实现,您的视图有两个侦听器。在您的情况下,两种实现方式都不同。延迟不会影响其他监听器的执行。

    【讨论】:

      【解决方案2】:

      onTextChanged 方法是一种受保护的方法,它的作用类似于TextView 子类的内部侦听器。默认实现是空的,它在文本更改时和侦听器之后被调用。所以它完全独立于听众。

      如果你想延迟听众(而不是改变文本),你可以这样做。但我不推荐它,因为我认为它会导致问题,尤其是在 afterTextChanged 中(因为它可以更改文本)。

          override fun addTextChangedListener(watcher: TextWatcher) {
              super.addTextChangedListener(object : TextWatcher {
                  override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
                      doDelay {
                          watcher.beforeTextChanged(s, start, count, after)
                      }
                  }
      
                  override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                      doDelay {
                          watcher.onTextChanged(s, start, before, count)
                      }
                  }
      
                  override fun afterTextChanged(s: Editable?) {
                      doDelay {
                          watcher.afterTextChanged(s)
                      }
                  }
      
              })
          }
      

      【讨论】:

        猜你喜欢
        • 2019-04-20
        • 1970-01-01
        • 1970-01-01
        • 2021-10-08
        • 1970-01-01
        • 1970-01-01
        • 2016-06-08
        • 2012-09-26
        • 1970-01-01
        相关资源
        最近更新 更多