【问题标题】:how to set color for string in EditText如何在EditText中为字符串设置颜色
【发布时间】:2014-12-12 07:44:23
【问题描述】:

我将字符串附加到编辑框。 这里 appendToMessageHistory 是方法。 我想为字符串用户名和消息设置单独的颜色
请在您投反对票之前给出解决方案,并认为这与 android 无关。

 public void appendToMessageHistory(String username, String message) {
            if (username != null && message != null) {
                messageHistoryText.append(username + ":");
                messageHistoryText.append(message + "\n" + "\n");
            }
        }

任何建议表示赞赏。

【问题讨论】:

标签: android


【解决方案1】:
String htmlText = "<font color=#cccccc>" + username+"</font> <font color=#ffeecc>"+ message+"</font>";
messageHistoryText.setText(Html.fromHtml(htmlText));

试试这个希望它会有所帮助。

public static Spanned fromHtml (String source)

【讨论】:

  • 很高兴听到它有帮助
【解决方案2】:

尝试使用SpannableString

SpannableString spannableString = new SpannableString(editText.getText().toString());
int lastIndex = //index to be used
spannableString.setSpan(new ForegroundColorSpan(Color.BLUE),0,lastIndex, 0);
editText.setText(spannableString);

看看这个不错的Tutorial.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多