select text in to edittext

2019-09-07 08:04发布

问题:

I am working on an Android application. In my app I am facing a strange problem.Please have a look on my screen.

when i select any thing from selction box then it will add twice to the edittext.

the following is my code for edittext

snd_txt.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            System.out.println("onTextChanged"+start+"b4"+before+"count"+count);
            if (start>0) {

                    int m=snd_txt.getText().toString().lastIndexOf(".");
                    makeColorText(m,snd_txt.getText().toString().length());

            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });




public void makeColorText(int num,int last) {

    int start=last-44;
    if (start<1){
        start=0;
    }
     SpannableString ss = new SpannableString(snd_txt.getText());

     ss.setSpan(new ForegroundColorSpan(R.color.Gray_Light), 0, start,0);
     ss.setSpan(new ForegroundColorSpan(Color.BLACK), start, last, 0); 
    // customize ss here
    // ...



    snd_txt.setText(ss);
    snd_txt.setSelection(last);
}

If i comment the snd_txt.setText(ss); statement .the the edittext will work perfectly. But I can't do that.I used that method to change edittext color at runtime. working fine for all the time..But at the time of selecting predictions will add the word two times.

Anybody have any idea to solve, Please help me.

Edit: How can I create a custom edittext with multicolor text.. for eg: current line in blue and previous are black.something like that

回答1:

You are doing a think which can causes a Exception due to recycling. You are watching for text change event of edittext and if there is a text change then you are again setting a text on the EditText so again your text change event will be occurred and you will again set the text so it will continue and it will probably give you Stack overflow exception or hang your UI.

You should use beforeTextChanged event and modify the CharSequence which you are getting in argument. Probably this will work for you.



回答2:

Solved my problem by changing my code.

snd_txt.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if (start>0) {
                 int m=snd_txt.getText().toString().lastIndexOf(".");
         makeColorText(snd_txt.getText().toString().length(),count);    
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
        }
    });

public void makeColorText(int last,int count) {

    int start=last-44;
    if (start<1){
        start=0;
    }

    String s=snd_txt.getText().toString();

    if (count==before_val&count!=1) {

        s=s.substring(0, last-before_val);
        last=last-before_val;
    }

     SpannableString ss = new SpannableString(s);

     ss.setSpan(new ForegroundColorSpan(R.color.Gray_Light), 0, start,0);
     ss.setSpan(new ForegroundColorSpan(Color.BLACK), start, last, 0); 
    // customize ss here
    // ...



         snd_txt.setText(ss);


     before_val=count;


    snd_txt.setSelection(last);
}