TextWatcher called even if text is set before adding the watcher

前端 未结 2 1555
鱼传尺愫
鱼传尺愫 2020-12-20 18:32

in an android activity, i first recover the text in an EditText and add then a TextWatcher to it.

private static int WC = 0;
public void onCreate(Bundle save         


        
相关标签:
2条回答
  • 2020-12-20 18:58

    Solution is to move your addTextChangedListener to onPostCreate method. all will be solved.

    0 讨论(0)
  • 2020-12-20 19:00

    This definitely must be happening. You are setting text when savedinstanceState is not null (meaning your previous object states are saved).

    I think that this happens because you added the TextWatcher to your EditText in the onCreate method, and next time onCreate gets called (e.g. after a configuration change) then it finds that the TextWatcher has already been added to the EditText.

    If you want to check for this situation, put this before your condition:

    if(savedInstanceState == null){
        Log.e("savedInstance state is null", "no text");
        et.setText("No text");
    }
    

    In this case, if you call setText on your EditText, then afterTextChanged(Editable s) will not be called.

    0 讨论(0)
提交回复
热议问题