3

如果用户键入一个字符串并且它包含 @ 我想将文本的颜色更改为红色。我曾尝试使用 textwatcher,但出现堆栈溢出错误。我只想在 @ 开头时更改颜色。代码如下

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>")));
            }

            
        }
    });
4

6 回答 6

2

在设置新文本以编辑文本之前,您必须删除 textwatcher。试试这个代码

           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);

    }
};
于 2014-09-16T07:06:33.780 回答
1
if (s.toString().matches("(@\\w+)")) 
{
      topic.setTextColor(Color.parseColor("#ffff0000"));
}
于 2013-08-05T09:52:08.287 回答
0
if(s.toString().startsWith("@"))
{
   topic.setTextColor(Color.RED);
}
于 2013-08-05T09:56:04.107 回答
0

尝试这个:

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>"));
        }


    }
});
于 2013-08-05T09:53:24.707 回答
0

这是一个 textWatcher 方法

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);
            }    
        }
    };
}
于 2019-01-07T21:13:42.213 回答
0

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);

        }
    });
于 2016-02-03T09:42:55.137 回答