0

In my test app, I wrote in onCreate

edit1 = (EditText) findViewById(R.id.editText_test1);
edit1.addTextChangedListener(new edittext_TextWatcher());

This is my textwatcher

public class edittext_TextWatcher implements TextWatcher{    
  @Override
  public void afterTextChanged(Editable s) {    
 // TODO Auto-generated method stub    
 String str=s.toString();    
 if(str.equals(""))
    Log.e("tag", "no");
 else
     Log.e("tag", str);
}    
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
// TODO Auto-generated method stub
}    
@Override
public void onTextChanged(CharSequence s, int start, int before,int count) {
// TODO Auto-generated method stub              
}           
}

When the activity starts, it prints the log below

tag------no

but when I add this code below edit1.addTextChangedListener(new edittext_TextWatcher()):

edit1.setText("0000");
edit1.setText("1111");
edit1.setText("2222");

tag------0000
tag------1111
tag------2222
tag------2222

The last one is repeated!
who can tell me the reason,why?

ohmygod
  • 1
  • 1
  • check this link http://stackoverflow.com/questions/5151095/textwatcher-called-even-if-text-is-set-before-adding-the-watcher – Jigar Shekh Feb 14 '14 at 13:07
  • I have read it,onPostCreate not work. I have tested,if I add addTextChangedListener after activity has stared,things no happened.I guess there is one event is worked after onCreate onStart and onResume – ohmygod Feb 14 '14 at 13:24
  • see what Thread.dumpStackTrace() says – pskink Feb 14 '14 at 13:24
  • I traced the logs: "tag------0000 tag------1111 tag------2222" is happened in oncreate.but the last one tag------2222" is happened after oncreate – ohmygod Feb 14 '14 at 15:18

0 Answers0