onTextChanged vs afterTextChanged in Android - Live examples needed

我与影子孤独终老i 提交于 2019-12-04 16:37:19

问题


I was reading about TextWatcher in Android programming. I could not understand the difference between afterTextChanged() and onTextChanged().

Although I referred to Differences between TextWatcher 's onTextChanged, beforeTextChanged and afterTextChanged, I am still not able to think of a situation when I would need to use onTextChanged() and not afterTextChanged().


回答1:


I found an explanation to this on 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.

So, the differences between the two are:

  • I can change my text using afterTextChanged while onTextChanged does not allow me to do that
  • onTextChanged gives me the offset of what changed where, while afterTextChanged does not



回答2:


Just adding something to Pratik Dasa's answer and the discussion with @SimpleGuy in the comments, since I have not enough reputation to comment.

The three methods are also triggered by EditText.setText("your string here"). That would make a length of 16 (in this case), so count isn't always 1.

Please note that the parameter list is not the same for the three methods:

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)

And this is where the difference is between afterTextChanged and onTextChanged: the parameters.

Please also have a look at the accepted answer in this thread: Android TextWatcher.afterTextChanged vs TextWatcher.onTextChanged




回答3:


here is the explanation:

onTextChanged : This means when you start typing, like you want to write "sports" then this will call on each and every character, like it will call when you pressed "s" then again "p" then "o" and so on...

afterTextChanged : This will call when you stop typing, it will calls after you completely wrote "sport", that is the main diffrence.

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
                        }
                    }
                });


来源:https://stackoverflow.com/questions/26992407/ontextchanged-vs-aftertextchanged-in-android-live-examples-needed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!