TextWatcher fires multiple times when entering return key into EditText

I have an EditText with a TextWatcher.


Scenario 1:

EditText containing " abcd "

If I press the return button or enter a new line

1) TextWatcher

fires 3 times before symbols .

2) between characters, TextWatcher

works 4 times.

3) at the end of characters it TextWatcher

fires 1 time.


Scenario 2:

EditText containing " 1234 "

If I press the return button or enter a new line

1) TextWatcher

fires 1 time before symbols .

2) between characters, it TextWatcher

works 1 time.

3) at the end of characters it TextWatcher

fires 1 time.


This is mistake?

Or is there something I don't understand?


I want the text observer to fire only once for the entire script.

Any help would be much appreciated.

+3


source to share


2 answers


I found a solution but may not be perfect for all needs.

Earlier , when it TextWatcher

worked several times, and the code in it also executed several times, I was with

editText.addTextChangedListener(new TextWatcher() {

    public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {

        Log.e(TAG, "111 text =---------------" + charSequence);
    }

    public void onTextChanged(CharSequence charSequence, int start, int before, int count){

        Log.e(TAG, "222 text =---------------" + charSequence);
    }

    public void afterTextChanged(Editable editable) {

        Log.e(TAG, "333 text ---------------" + editable);
    }
});

      



Now, according to my requirements, I found a solution and I am with

editText.addTextChangedListener(new TextWatcher() {

    String initialText = "";
    private boolean ignore = true;

    public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {

        if ( initialText.length() < charSequence.length() ){

            initialText = charSequence.toString();
            Log.e(TAG, "111 text ---------------" + charSequence);
        }
    }

    public void onTextChanged(CharSequence charSequence, int start, int before, int count){

        if( initialText.length() < charSequence.length() ) {

            initialText="";
            ignore=false;
            Log.e(TAG, "222 text ---------------" + charSequence);
        }
    }

    public void afterTextChanged(Editable editable) {

        if(!ignore) {

            ignore = true;
            Log.e(TAG, "333 text ---------------" + editable);
        }
    }
});

      

Now also <<20> fires multiple times , but the code in the if conditions is only executed once for all the scenarios I mentioned in my question.

+1


source


This is because numbers are counted as a single value ie, 1 or 12 "twelve" instead of 1.2. Conversely, when you enter the words "Strings", they are divided into characters, and the total number of characters in the entire string is returned in the count parameter of the overloaded textWatcher methods.

For example, if you enter 123, it will be interpreted as one value one hundred twenty three. Hence, the score is returned as 1. And when you type hello, it divides into individual characters, ie "h", "e", "l", "l", "o", which is only 5 characters long. Hence, the total is returned as 5.



Hope this explanation helps.

0


source







All Articles