0

是否可以使用 if 条件为 TextView 内的不同文本设置多种颜色?

这是我的代码:

mColoredText = findViewById(R.id.questionText);
        String mColoredString = "BLACK RED GREEN YELLOW ORANGE BLUE WHITE";
        SpannableStringBuilder builder = new SpannableStringBuilder();

        if(mColoredString.contains("RED")) {
            String red = "RED";
            SpannableString redSpannable = new SpannableString(red);
            redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0);
            builder.append(redSpannable);
        }

        if(mColoredString.contains("YELLOW")) {
            String yellow = "YELLOW";
            SpannableString whiteSpannable = new SpannableString(yellow);
            whiteSpannable.setSpan(new ForegroundColorSpan(Color.YELLOW), 0, yellow.length(), 0);
            builder.append(whiteSpannable);
        }

        if(mColoredString.contains("BLUE")) {
            String blue = "BLUE";
            SpannableString blueSpannable = new SpannableString(blue);
            blueSpannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, blue.length(), 0);
            builder.append(blueSpannable);
        }

        mColoredText.setText(builder, TextView.BufferType.SPANNABLE);

但最终的结果总是打印:RED YELLOW BLUE 和它的颜色,只有三个文本。

我希望 BLACK RED GREEN YELLOW ORANGE BLUE WHITE 写在一起,如果没有可扩展的颜色,则适用白色。

4

2 回答 2

0

您创建一个新的构建器并只添加红色黄色和蓝色。这就是为什么你总是看到红黄蓝。

如果要更新原始文本,则必须SpannableString先从中创建一个。

SpannableString mColoredString = new SpannableString("BLACK RED GREEN YELLOW ORANGE BLUE WHITE");

if (mColoredString.toString().contains("RED")) {
    mColoredString.setSpan(new ForegroundColorSpan(Color.RED), 6, 9, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (mColoredString.toString().contains("YELLOW")) {
    mColoredString.setSpan(new ForegroundColorSpan(Color.YELLOW), 16, 22, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (mColoredString.toString().contains("BLUE")) {
    mColoredString.setSpan(new ForegroundColorSpan(Color.BLUE), 30, 34, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

questionText.setText(mColoredString);

请阅读这篇文章以获得详细的解释。

编辑

如果您不知道startend位置,那么您必须计算它们:

if (mColoredString.toString().contains("YELLOW")) {
    int start = mColoredString.toString().indexOf("YELLOW");
    int end = start + "YELLOW".length();
    mColoredString.setSpan(new ForegroundColorSpan(Color.YELLOW), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
于 2018-06-25T07:07:02.287 回答
0

试试这个代码..

    textView = findViewById(R.id.tvData);
    String mColoredString = "BLACK RED GREEN YELLOW ORANGE BLUE WHITE";
    SpannableStringBuilder builder = new SpannableStringBuilder();

    String strArray[] = mColoredString.split(" ");
    for (int i = 0; i < strArray.length; i++) {
        if (strArray[i].equals("RED")) {
            SpannableString redSpannable = new SpannableString(strArray[i]);
            redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, strArray[i].length(), 0);
            builder.append(redSpannable);
        } else if (strArray[i].equals("YELLOW")) {
            SpannableString whiteSpannable = new SpannableString(strArray[i]);
            whiteSpannable.setSpan(new ForegroundColorSpan(Color.YELLOW), 0, strArray[i].length(), 0);
            builder.append(whiteSpannable);
        } else if (strArray[i].equals("BLUE")) {
            SpannableString blueSpannable = new SpannableString(strArray[i]);
            blueSpannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, strArray[i].length(), 0);
            builder.append(blueSpannable);
        } else {
            builder.append(strArray[i]);
        }

    }
    textView.setText(builder);

}
于 2018-06-25T07:35:18.860 回答