Android EditText改变单个单词的颜色

时间:2014-10-16 05:47:25

标签: android

如何更改EditText中单个单词的颜色?意思是所有单词都是一次颜色,一个单词将是不同的颜色。

亲切的问候

3 个答案:

答案 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)

是的,你可以像下面这样做

enter image description here

您也可以为EditText实现此功能。请参阅here完整详细信息教程。

我希望这能解决你的问题。

答案 2 :(得分:1)

试试这个。

String text = "<font color=#0000ff>sample</font> <font color=#ff0000>text</font>";
    edittext.setText(Html.fromHtml(text));