2

如果单词以 # 开头,则在编辑文本中输入时我有一个编辑文本,该特定单词的颜色应该改变,

我已经实现了textwatcher,发现如果文本以#开头但不知道如何动态更新颜色,

已经尝试过SpannableStringBuilder ssb = new SpannableStringBuilder(yourText)但是它是静态的,任何人都可以帮助我进行动态实现

这是我的代码

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

        }

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

           if (text.charAt(start) == '#') {
               //here i needs to update the typing text color
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
4

4 回答 4

0

如此链接中实施的如何使用主题标签更改单词的颜色@Muhammed GÜNEŞ 建议

您可以根据以下内容进行修改

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


        textView.removeTextChangedListener(this);
        setTags(textView,s.toString());
        textView.setSelection(textView.getText().toString().length());
        textView.addTextChangedListener(this);
    }


private void setTags(TextView pTextView, String pTagString) {
        SpannableString string = new SpannableString(pTagString);

        int start = -1;
        for (int i = 0; i < pTagString.length(); i++) {
            if (pTagString.charAt(i) == '#') {
                start = i;
            } else if (pTagString.charAt(i) == ' ' || (i == pTagString.length() - 1 && start != -1)) {
                if (start != -1) {
                    if (i == pTagString.length() - 1) {
                        i++; // case for if hash is last word and there is no
                        // space after word
                    }

                    final String tag = pTagString.substring(start, i);
                    string.setSpan(new ClickableSpan() {

                        @Override
                        public void onClick(View widget) {
                            Toast.makeText(MainActivity.this,"Click",Toast.LENGTH_LONG).show();
                        }

                        @Override
                        public void updateDrawState(TextPaint ds) {
                            // link color
                            ds.setColor(Color.parseColor("#33b5e5"));
                            ds.setUnderlineText(false);
                        }
                    }, start, i, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    start = -1;
                }
            }
        }

        pTextView.setMovementMethod(LinkMovementMethod.getInstance());
        pTextView.setText(string);
    }

它也会添加颜色和单击事件,您也可以根据您的进一步要求对其进行修改

于 2017-05-23T13:43:07.920 回答
0

终于找到答案并按预期工作。

 private int intCount = 0, initialStringLength = 0;
private String strText = "";

文字改变

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

            String strET = editText.getText().toString();
            String[] str = strET.split(" ");
            int cnt = 0;
            if (text.length() != initialStringLength && text.length() != 0) {
                if (!strET.substring(strET.length() - 1).equals(" ")) {
                    initialStringLength = text.length();
                    cnt = intCount;
                    for (int i = 0; i < str.length; i++)
                        if (str[i].charAt(0) == '#')
                            strText = strText + " " + "<font color='#EE0000'>" + str[i] + "</font>";
                        else
                            strText = strText + " " + str[i];
                }
                if (intCount == cnt) {
                    intCount = str.length;
                    editText.setText(Html.fromHtml(strText));
                    editText.setSelection(textShareMemories.getText().toString().length());
                }
            } else {
                strText = "";
            }

        }
于 2017-05-24T05:44:07.050 回答
0

也许是这样的

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

       if (text.charAt(start) == '#') {

           SpannableString spannableString = new SpannableString(editText.getText().toString());
           ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.yourColor));
           spannableString.setSpan(foregroundSpan, start, 
           spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
           editText.setText(spannableString);

        }
    }
于 2017-05-23T13:19:14.047 回答
0

如果您只想更改“#”字符。你可以试试:

int index = myEditTxt.getText().toString().indexOf("#");
if (index != -1) {
   SpannableString spannable = new     
   SpannableString(myEditTxt.getText().toString());
   ForegroundColorSpan fcs = new ForegroundColorSpan(color);
   // Set the text color
   spannable.setSpan(fcs, index, index + 1, 
      Spannable.SPAN_INCLUSIVE_INCLUSIVE);

   mEditText.setText(spannable);
}    

如果您只想在“#”之后和之前更改。您可以在侦听器中尝试:

if (text.charAt(start) == '#') {
   SpannableString spannable = new     
   SpannableString(myEditTxt.getText().toString());
   ForegroundColorSpan fcs = new ForegroundColorSpan(color);
   // Set the text color
   if (start > 0)
         spannable.setSpan(fcs, 0, start -1, 
      Spannable.SPAN_INCLUSIVE_INCLUSIVE);
   if (start < myEditTxt.getText().toString().length())
         spannable.setSpan(fcs, start + 1, myEditTxt.getText().toString().length(), 
      Spannable.SPAN_INCLUSIVE_INCLUSIVE);

   mEditText.setText(spannable);
}
于 2017-05-23T13:26:21.163 回答