【问题标题】:Change color to odd words from a String将颜色更改为字符串中的奇数词
【发布时间】:2014-10-24 09:14:28
【问题描述】:

我正在做一个小项目,但我无法获得预期的结果。我想改变一个字符串的颜色奇数词和另一个颜色。任何人都可以帮忙吗?我在这里留下我当前的代码:

   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);
    }

您好!

【问题讨论】:

  • 看看here
  • 谢谢@SteveBenett,它的作品! ;-)

标签: java android arrays string colors


【解决方案1】:

如果要将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));

【讨论】:

    猜你喜欢
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    • 2012-04-13
    • 2019-03-12
    • 2011-06-05
    相关资源
    最近更新 更多