2

My purpose is that initial my edittext view as 0.00. After enter one number ex-2 so then edit text should show as - 0.02 Then I enter second one ex- 3 edit text should show - 0.23 After the next number ex -5 edit text should show - 2.35 for this scenario I have implemented small code.But that is not working as expected . Edittext does not update.

    public class MainActivity extends Activity {

    private EditText text;
    Character[] amount;
    int counter=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (EditText)findViewById(R.id.text);
        text.setSelection(text.getText().length());
        amount =toCharacterArray(text.getText().toString());

        text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                System.out.println("test "+s);
                if(s.length()>0)
                {

                    if(counter==0 && s.length()>=5)
                    {
                        Log.e("Edit counter 1",String.valueOf(s) );
                        amount[3]=s.charAt(4);
                    }
                    else if (counter==1&& s.length()>=5)
                    {
                        Log.e("Edit counter 2",String.valueOf(s) );
                        amount[2]=amount[3];
                        amount[3]=s.charAt(4);
                    }
                    else if (counter==2 && s.length()>=5)
                    {
                        amount[0]=amount[2];
                        amount[2]=amount[3];
                        amount[3]=s.charAt(4);
                    }
                    else
                    {

                    }


                    String str = "";

                    for (Character c : amount)
                        str += c.toString();

                    System.out.println("Edit test "+str);


                   text.setText(str);

                }
                counter++;

            }

            @Override
            public void afterTextChanged(Editable s) {
                System.out.println(s.toString());


            }

        });
    }
    public Character[] toCharacterArray( String s ) {

        if ( s == null ) {
            return null;
        }

        int len = s.length();
        Character[] array = new Character[len];
        for (int i = 0; i < len ; i++) {
            array[i] = new Character(s.charAt(i));
        }

        return array;
    }
}

2 Answers 2

3

When you call

text.setText(str);

inside onTextChanged callback, that TextView will change text one more time, then onTextChanged will be called again. It will cause an infinite loop there.

edit:

You can add a bool flag for this case. Ex: isUserInput.

    text.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            System.out.println("test "+s);
            if(s.length()>0 && isUserInput)
            {

                if(counter==0 && s.length()>=5)
                {
                    Log.e("Edit counter 1",String.valueOf(s) );
                    amount[3]=s.charAt(4);
                }
                else if (counter==1&& s.length()>=5)
                {
                    Log.e("Edit counter 2",String.valueOf(s) );
                    amount[2]=amount[3];
                    amount[3]=s.charAt(4);
                }
                else if (counter==2 && s.length()>=5)
                {
                    amount[0]=amount[2];
                    amount[2]=amount[3];
                    amount[3]=s.charAt(4);
                }
                else
                {

                }


                String str = "";

                for (Character c : amount)
                    str += c.toString();

                System.out.println("Edit test "+str);


               text.setText(str);
               isUserInput = false;
            }else{
               isUserInput = true;
            }
            counter++;

        }
4
  • Tin Nguyen- Yes exactly.So how can I update my edittext after doing modification ? Same result got once I tried to update edittext in afterTextChanged method? May 24, 2018 at 7:29
  • Hi, I got same result.infinite loop there .after adding that flag also. May 24, 2018 at 8:48
  • Small change should be done. isUserInput = false; text.setText(str); thses two lines should be swaped. May 24, 2018 at 9:58
  • Oh, Sorry. I didn't notice that.
    – Tin Nguyen
    May 24, 2018 at 10:35
1

you can reserve a flag string to indicate whether the coming Editable is from UI Input or your code.

       findViewById<EditText>(R.id.editText).addTextChangedListener(object : TextWatcher {

        var lastString: String = ""

        override fun afterTextChanged(s: Editable?) {
            if (s.toString() == lastString) {
                return
            }
            //customized
            lastString = "${s.toString()}1"
            s?.replace(0, s.toString().length, lastString)
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

        }

    })
1
  • Use @Tin Nguyen's answer,a bool variable is better than a string, i think.
    – wkm
    May 24, 2018 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.