将字符串中的颜色更改为奇数字

时间:2014-08-30 15:17:35

标签: java android arrays string colors

我正在处理一个小项目,但我无法获得预期的结果。我想改变字符串的颜色奇数字和另一种颜色。有人可以帮忙吗?我离开这里是我现在的代码:

   public void updateMessages() {
        String groupMessage = "Admin says: Hi people\n Admin says: What's up?";

        TextView groupMessageBox = (TextView) this
                .findViewById(R.id.groupMessageBox);

        String[] str_array = groupMessage.split("\n|\\:");

        //Result str_array == Admin says, Admin says

        for (int i = 0; i < str_array.length; i++) {
//Get values of array           
String val1 = str_array[i];


//Only numbers pairs
if ( i % 2 == 0 ) { 
                //How to change the result to blue, for example?
                  Log.e("",val1);
            }else{
                  Log.e("",val1);
                //How to change the result to red, for example?
            }
        }
        groupMessageBox.setText(groupMessage);
    }

问候!

1 个答案:

答案 0 :(得分:0)

如果要将val1的颜色更改为红色,例如:

String coloredString = "<font color="#FF0000">" + val1 + "</font>";
str_array[i] = coloredString;

然后在for循环之外,你可能想重新加入文本并重置textView:

String coloredMessage = // Join the strings together
groupMessageBox.setText(Html.fromHtml(coloredMessage));