一个TextView使用不同的颜色

一个TextView使用不同的颜色:

textview中首先需要已经有内容了

public static void ColorFormatTextView(TextView tv,int color,String textStr){
        if (TextUtils.isEmpty(textStr) || tv == null
                || TextUtils.isEmpty(tv.getText())) {
            return;
        }
        String showString = tv.getText().toString();
        int startIndex = showString.indexOf(textStr);
        if (startIndex < 0) {
            return;
        }
        ForegroundColorSpan style = new ForegroundColorSpan(color);
        SpannableStringBuilder formatted = new SpannableStringBuilder(
                showString);
        formatted.setSpan(style, startIndex, startIndex + textStr.length(),
                Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        tv.setText(formatted);
     }
原文地址:https://www.cnblogs.com/qgli/p/4652016.html