【问题标题】:onTextChanged vs afterTextChanged in Android - Live examples neededAndroid 中的 onTextChanged 与 afterTextChanged - 需要实时示例
【发布时间】:2014-11-18 10:48:03
【问题描述】:

我正在阅读有关 Android 编程中的 TextWatcher 的信息。我无法理解afterTextChanged()onTextChanged() 之间的区别。

虽然我提到 Differences between TextWatcher 's onTextChanged, beforeTextChanged and afterTextChanged,我仍然无法想到需要使用onTextChanged() 而不是afterTextChanged() 的情况。

【问题讨论】:

    标签: android android-textwatcher


    【解决方案1】:

    我在 Android Dev Portal 上找到了对此的解释

    http://developer.android.com/reference/android/text/TextWatcher.html

    **abstract void afterTextChanged(Editable s)**
    This method is called to notify you that, somewhere within s, the text has been changed.
    
    **abstract void beforeTextChanged(CharSequence s, int start, int count, int after)**
    This method is called to notify you that, within s, the count characters beginning at start are about to be replaced by new text with length after.
    
    **abstract void onTextChanged(CharSequence s, int start, int before, int count)**
    This method is called to notify you that, within s, the count characters beginning at start have just replaced old text that had length before.
    

    所以,两者的区别在于:

    • 我可以使用afterTextChanged更改我的文本,而onTextChanged 不允许我这样做
    • onTextChanged 为我提供了更改位置的偏移量,而 afterTextChanged 没有

    【讨论】:

      【解决方案2】:

      只是在 Pratik Dasa 的回答和 cmets 中与 @SimpleGuy 的讨论中添加一些内容,因为我没有足够的声誉来发表评论。

      这三个方法也是EditText.setText("your string here")触发的。这将使长度为 16(在这种情况下),因此 count 并不总是 1

      请注意,这三种方法的参数列表是不一样的:

      abstract void afterTextChanged(Editable s)
      abstract void beforeTextChanged(CharSequence s, int start, int count, int after)
      abstract void onTextChanged(CharSequence s, int start, int before, int count)
      

      这就是afterTextChangedonTextChanged 之间的区别:参数。

      还请查看此线程中接受的答案:Android TextWatcher.afterTextChanged vs TextWatcher.onTextChanged

      【讨论】:

      • 另外,当将一些文本粘贴到 EditText 时,count 不会是 1。
      【解决方案3】:

      解释如下:

      onTextChanged :这意味着当您开始输入时,就像您想写“sports”一样,这将调用每个字符,就像您按“s”然后再按“p”然后“o”和等等……

      afterTextChanged : 停止输入时调用,写完“sport”后调用,主要区别。

      YOUR_EDIT_TEXT.addTextChangedListener(new TextWatcher() {
      
                          @Override
                          public void onTextChanged(CharSequence s, int start, int before, int count) {
      
                          //Your query to fetch Data
                          }
      
                          @Override
                          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
      
                          }
      
                          @Override
                          public void afterTextChanged(Editable s) {
                              if (s.length() > 0) {
      
                                  //Your query to fetch Data
                              }
                          }
                      });
      

      【讨论】:

      • 但是,它怎么知道我停止打字了?我暂停(甚至是纳秒),但这是一个暂停.. android 不知道我要输入“s”“p”.. 等等.. 它怎么知道我要进一步输入..就像我输入“spor”.. 完成.. 那么它会被调用吗?或者说我输入“运动”然后它会被调用..如果是..然后它被调用两次..抱​​歉无法理解差异
      • @SimpleGuy 当然,Android 知道这一点,操作系统显然有一些标准来获取输入字符和使用它之间的延迟可以得出结论。 Android 必须有一些标准来识别它。
      • 我在 android 开发者门户网站上阅读。 [link](developer.android.com/reference/android/text/…, int, int, int)) 它说 onTextChanged -“调用此方法是为了通知您,在 s 内,从 start 开始的计数字符刚刚被替换” - 为什么“字符”(复数)当你说它是为每个字符?如果您说每个字符都调用它(count = 1),为什么根本需要count
      • @SimpleGuy Docs 是对的,在 onTextChanged() 方法中,当您键入字符时,它会将每个字符替换为之前键入的字符。对!!!
      • 根据现在的一些测试,这个答案(“停止输入时会触发”)不正确。两种方法同时触发,并且具有相同的 s.toString() 内容
      猜你喜欢
      • 2013-12-15
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      相关资源
      最近更新 更多