Android TextWatcher.afterTextChanged vs TextWatcher.onTextChanged
These events are called in the following order:
beforeTextChanged(CharSequence s, int start, int count, int after).
This means that the characters are about to be replaced with some new text. The text is uneditable.
Use: when you need to take a look at the old text which is about to change.onTextChanged(CharSequence s, int start, int before, int count).
Changes have been made, some characters have just been replaced. The text is uneditable.
Use: when you need to see which characters in the text are new.afterTextChanged(Editable s).
The same as above, except now the text is editable.
Use: when you need to see and possibly edit the new text.
If I'm just listening for changes in EditText
, I won't need to use the first two methods at all. I will just receive new values in the third method and correct new text if needed. However, if I had to track down exact changes which happen to the values, I would use the first two methods. If I also had a need to edit the text after listening to the changes, I would do that in the third method.
onTextChanged vs afterTextChanged in Android - Live examples needed
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
whileonTextChanged
does not allow me to do that - onTextChanged gives me the offset of what changed where, while afterTextChanged does not
Differences between TextWatcher 's onTextChanged, beforeTextChanged and afterTextChanged
onTextChanged
runs during the text changing.
afterTextChanged
runs immediately after the text is changed.
beforeTextChanged
runs the instant before the text is changed.
Depending on when you want to assign variables or do things, you may want to run the code the instant before the change, or the instant after.
Here is an example of this:
String afterTextChanged = "";
String beforeTextChanged = "";
String onTextChanged = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText)findViewById(R.id.editText);
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int st, int b, int c)
{
onTextChanged = et.getText().toString();
}
@Override
public void beforeTextChanged(CharSequence s, int st, int c, int a)
{
beforeTextChanged = et.getText().toString();
}
@Override
public void afterTextChanged(Editable s)
{
afterTextChanged = et.getText().toString();
Toast.makeText(Activity.this, "before: " + beforeTextChanged
+ '\n' + "on: " + onTextChanged
+ '\n' + "after: " + afterTextChanged
,Toast.LENGTH_SHORT).show();
}
});
}
In this case, let's say you changed the text from "h" to "hi", the output would be:
before: "h"
on: "hi"
after: "hi"
How can I change the EditText text without triggering the Text Watcher?
You could unregister the watcher, and then re-register it.
Alternatively, you could set a flag so that your watcher knows when you have just changed the text yourself (and therefore should ignore it).
Related Topics
Export a Python Script to an Android Executable (.Apk) with Python-For-Android
How to Create a Table with Borders in Android
How to Marshall and Unmarshall a Parcelable to a Byte Array with Help of Parcel
Android Set Up Volley to Use from Cache
Locationsettingsrequest Dialog to Enable Gps - Onactivityresult() Skipped
How to Strip or Escape HTML Tags in Android
Android 4.3 Bluetooth Low Energy Unstable
What Should I Use Android Accountmanager For
Difficulties with Calling an Android Ndk Function from Directly Delphi
No Shadow by Default on Toolbar
How to Fix: "Hax Is Not Working and Emulator Runs in Emulation Mode"
How to Request Permissions from a Service in Android Marshmallow
Get Android API Level of Phone Currently Running My Application
Android Dex Gives a Bufferoverflowexception When Building