Reputation: 1009
I am learning how to write applications for android and have a question. What is the difference between using a TextWatcher and within the the onTextChanged Method, setting a string value equal to the CharSequence argument and simply using getText method.
private TextWatcher passwordListener = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
try{
password = charSequence.toString();
}
catch(Exception e){
password=null;
}
}
vs
password = password_EditText.getText().toString();
Upvotes: 0
Views: 973
Reputation: 29
TextWatcher can dynamic listener text changge , more Powerful text processing。 getText() is static get.
Upvotes: 0
Reputation: 304
You would use a text watcher if you want to be notified that the text has been changed and need to do something in direct response to that event itself. For example if you wanted to do some custom validation on the fly and automatically enable / disable a button based on the value entered.
Using getText just simply returns what ever is in the edit text field at that particular time. You might use this if you want to get the text value as a result of some other event like a button click.
Upvotes: 1