Set text color for a specific text in Android Edit

2019-07-18 01:05发布

If user types a String and it contains @ i want to change the color of the text to red.i have tried with textwatcher but got stack overflow error.I want to change the color only if the @ is at the beginning.The code is given below

topic.addTextChangedListener(new TextWatcher() {

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

        }

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

        }

        public void afterTextChanged(Editable s) {

            if (s.toString().matches("(@\\w+)")) {
                topic.setText(Html.fromHtml(s.toString().replaceAll(
                        "(@\\w+)", "<font color='#ffff0000'>$1</font>")));
            }


        }
    });

6条回答
Melony?
2楼-- · 2019-07-18 01:27

append to bring curser at end of line and set text null before appending

edt_customer_cc.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) {


        }

        @Override
        public void afterTextChanged(Editable s) {

            edt_customer_cc.removeTextChangedListener(this);

            String text = edt_customer_cc.getText().toString();

            if (text .contains(";")) {

                edt_customer_cc.setText("");

                // append to bring curser at end of line
                edt_customer_cc.append(Html.fromHtml(text.replaceAll(
                        "(\\;)", "<font color='#ff0000'>;</font>")));


            }
            //editText.setText(text);
            //editText.setSelection(text.length());
            edt_customer_cc.addTextChangedListener(this);

        }
    });
查看更多
再贱就再见
3楼-- · 2019-07-18 01:32
if(s.toString().startsWith("@"))
{
   topic.setTextColor(Color.RED);
}
查看更多
Evening l夕情丶
4楼-- · 2019-07-18 01:38

You have to remove the textwatcher before you set the new text to edit text. Try this code

           private TextWatcher textWatcher = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        editText.removeTextChangedListener(this);
        String text = editText.getText().toString();

         if (text .matches("(@\\w+)")) {
            editText.setText(Html.fromHtml(text .replaceAll(
                    "(@\\w+)", "<font color='#ffff0000'>$1</font>")));
        }

        editText.addTextChangedListener(this);

    }
};
查看更多
啃猪蹄的小仙女
5楼-- · 2019-07-18 01:45

Try this:

topic.addTextChangedListener(new TextWatcher() {

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

    }

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

    }

    public void afterTextChanged(Editable s) {

        if (s.toString().startsWith("@")) {
            topic.setText(
                Html.fromHtml("<font color='#ffff0000'>"+
                s.toString().substring(1)
                +"</font>"));
        }


    }
});
查看更多
贼婆χ
6楼-- · 2019-07-18 01:50
if (s.toString().matches("(@\\w+)")) 
{
      topic.setTextColor(Color.parseColor("#ffff0000"));
}
查看更多
小情绪 Triste *
7楼-- · 2019-07-18 01:52

This is a textWatcher Metod

public TextWatcher textWatcher(final EditText editText) {
    return new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
         if ("@".equals(s.substring(0, 1))) {
               editText.setTextColor(Color.RED);
            }    
        }
    };
}
查看更多
登录 后发表回答