根据条件以编程方式更改字符串颜色
change string color programmatically depending if condition
是否可以使用 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);
但最终结果总是打印:红色黄色蓝色及其颜色,只有三个文本。
我希望BLACK RED GREEN YELLOW ORANGE BLUE WHITE都写在一起,如果没有spannable颜色就用白色。
您创建了一个新的构建器并仅向其中添加了红黄蓝两种颜色。这就是为什么你总是看到红黄蓝。
如果你想更新原文那么你必须先从它做一个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);
请阅读this article了解详细说明。
编辑:
如果您不知道 start
和 end
的位置,那么您必须计算它们:
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);
}
试试这个代码..
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);
}