Android EditText在键入时动态更改单个单词的颜色(动态)

时间:2017-05-23 13:07:35

标签: android android-edittext spannablestringbuilder

  

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

我已经实现了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 个答案:

答案 0 :(得分: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);

        }
    }

答案 1 :(得分: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);
}

答案 2 :(得分:0)

正如此链接How to change color of words with hashtags@MuhammedGÜ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);
    }

它也会添加颜色和点击事件,你也可以根据你的进一步要求修改它

答案 3 :(得分: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 = "";
            }

        }