Android EditText如何改变单个单词的颜色

3

如何更改EditText中单个单词的颜色?也就是说,所有单词都是同一种颜色,而一个单独的单词则是另一种颜色。

敬礼


1
尝试使用HTML格式化字符串。 - Remees M Syde
1
使用SpannableString - AndyN
3个回答

3
使用 ForegroundColorSpan
SpannableStringBuilder ssb = new SpannableStringBuilder(yourText);
ForegroundColorSpan colorSpan = new ForegroundColorSpan(
        context.getResources()
        // Specify your color
        .getColor(R.color.your_font_color));
realPrice.setSpan(colorSpan,
         0, // Start index of the single word
         1, // End index of the single word
         Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
yourEditText.setText(ssb);

1
尝试一下。
String text = "<font color=#0000ff>sample</font> <font color=#ff0000>text</font>";
    edittext.setText(Html.fromHtml(text));

1

是的,您可以像下面的图片一样做到这一点

enter image description here

你也可以将此应用于EditText。查看此处的完整详细教程。
我希望这能解决你的问题。

2
https://dev59.com/OXXYa4cB1Zd3GeqP_OCc#25874987 - 这里有一个关于EditText的可工作示例。 - slott
@slott。谢谢你,兄弟。我做了很多不必要的字符串修改,但从来没有真正搞定它。你分享的链接上的答案正中要害。如此简单而且完美地运行。 - Plain_Dude_Sleeping_Alone

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接