如何更改EditText中单个单词的颜色?意思是所有单词都是一次颜色,一个单词将是不同的颜色。
亲切的问候
答案 0 :(得分: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 :(得分:1)
答案 2 :(得分:1)
试试这个。
String text = "<font color=#0000ff>sample</font> <font color=#ff0000>text</font>";
edittext.setText(Html.fromHtml(text));