I have an Android programming question. Using the code below I want to validate a string match. It validates fine but LogCat is showing that the TextWatcher methods are firing twice per a keystroke and I can't figure out why. I would like for the firing to only occur once per keystroke.
Do you know why it's doing this?
I thought it might be because I change the color of the text but after commenting it out it didn't make a difference.
LogCat Output
03-31 03:37:25.269: I/BeforeText(676): Hit
03-31 03:37:25.269: I/OnText(676): Hit
03-31 03:37:25.269: I/AfterText(676): Hit
03-31 03:37:25.274: I/InvalidText(676): Incorrect Text.
03-31 03:37:25.274: I/Text Value(676): a
03-31 03:37:25.404: I/BeforeText(676): Hit
03-31 03:37:25.404: I/OnText(676): Hit
03-31 03:37:25.404: I/AfterText(676): Hit
03-31 03:37:25.404: I/InvalidText(676): Incorrect Text.
03-31 03:37:25.404: I/Text Value(676): a
Activity Code
public void onCreate(Bundle savedInstanceState) {
//...omitted
//Create Answer Field
textField = (EditText)this.findViewById(R.id.textField);
//Add validation to TextField
textField.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s){
Log.i("AfterText","Hit");
if(textField.getText().toString().trim().equalsIgnoreCase("hello")){
Log.i("ValidText", "Text matched.");
answerField.setTextColor(Color.GREEN);
}
else{
Log.i("InvalidText", "Incorrect text.");
Log.i("Text Value", textField.getText().toString());
textField.setTextColor(Color.RED);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){
//Do nothing
Log.i("BeforeText", "Hit");
}
public void onTextChanged(CharSequence s, int start, int before, int count){
//Do nothing
Log.i("OnText","Hit");
}
});
}