如何在两种不同类型的花括号之间更改文本的颜色

时间:2019-12-23 15:25:24

标签: android

我有一个字符串,例如:sed ':a;/plot/!{n;ba;};:b;n;s///;bb'

我想在花括号和带括号的括号之间更改颜色,我该怎么办?

1 个答案:

答案 0 :(得分:2)

这是通过创建如下所示的SpannableStringBuilder来完成的:

SpannableStringBuilder builder = new SpannableStringBuilder();

String red = "this is red";
SpannableString redSpannable= new SpannableString(red);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0);
builder.append(redSpannable);

String white = "this is white";
SpannableString whiteSpannable= new SpannableString(white);
whiteSpannable.setSpan(new ForegroundColorSpan(Color.WHITE), 0, white.length(), 0);
builder.append(whiteSpannable);

textView.setText(builder, BufferType.SPANNABLE);

有关更多信息,请查看here