2013-09-25 31 views
0

我有一个textview,我想将以@开头的单词的颜色更改为红色,并且如果还有#的单词,我想将其更改为黑色color.Text没有硬编码。我怎么做到这一点,请帮助我。我已经尝试了下面的代码。但问题是红色仅适用于第一个单词。带有两种不同颜色的动态文本的单个textview

String MsgTitle = Details.get(position).getContent(); 
       String refString; 
       String hashString; 
       if (MsgTitle.contains(" ")) { 
        refString = MsgTitle.substring(0, 
          MsgTitle.indexOf(' ')).trim(); 

        if (String.valueOf(refString.charAt(0)).equals("@")) { 
         refString = refString.replaceFirst("(^@\\w+)", 
           "<font color='#8A0A0A'>$1</font>"); 
        } else { 
         refString = refString.replaceAll("(#\\w+)", 
           "<font color='#000000'><b>$1</b></font>"); 
        } 

        hashString = MsgTitle.substring(
          MsgTitle.indexOf(' ') + 1).trim(); 
        ((TextView) vi.findViewById(R.id.textData)) 
          .setText(Html.fromHtml(refString 
            + " " 
            + hashString 
              .replaceAll("(#\\w+)", 
                "<font color='#000000'><b>$1</b></font>"))); 
} 

回答我的问题

tring[] str_array = str .split(" "); 

      boolean isExists = false; 
      for (int i = 0; i < str_array.length; i++) { 
       for (int j = 0; j < i; j++) { 
        if (str_array[j].equals(str_array[i])) { 
         isExists = true; 
        } 
       } 
       if (str_array[i].startsWith("@") && !isExists) { 
        str = str .replace(str_array[i], 
          "<font color='#8A0A0A'>" + str_array[i] 
            + "</font>"); 
       } else if (str_array[i].contains("#")) { 
        str = str .replaceAll(str_array[i], 
          "<font color='#000000'><b>" + str_array[i] 
            + "</b></font>"); 
       } 
      } 

回答

3

做到这一点的方式..

@Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      txtHeader = (TextView) findViewById(R.id.txtHeader); 
      String str = "hello as new kkk @lll llll kkkk mm nn dd #jjj @pppp"; 

      String[] str_array = str.split(" "); 

      for (int i = 0; i < str_array.length; i++) { 
       if (str_array[i].startsWith("@")) { 
        str=str.replaceAll(str_array[i], "<font color='red'>" + str_array[i] + "</font>"); 
       } else if (str_array[i].startsWith("#")) { 
        str=str.replaceAll(str_array[i], "<font color='black'>" + str_array[i] + "</font>"); 
       } 
      } 

      txtHeader.setText(Html.fromHtml(str)); 

     } 
+0

谢谢。它的工作。但一些文本显示为@ somt..ng和一些其他文本显示为@Something ..> – user1767260

+0

你可以在这里发布那个字符串,为你创建问题? –

+0

它只是改变textcolor不是字符串,所以有什么问题?实际上没有得到你的观点,你写的第一条评论.. –

0

您可以通过HTML格式字符串做到这一点:

String text = "<font color=#ff0000>red text</font> <font color=#00ff00>green color</font>"; 
tv.setText(Html.fromHtml(text)); 

你需要做一些编码,生成这是来自@和#标签的HTML文本,但这不应该太难。

祝你好运:)

编辑: 这里的东西应该解决您的算法。它只适用于寻找红色标签,但可以很容易地扩展为其他颜色:

(顺便说一下,我没有测试它,所以可能有一些小故障在这里和那里......希望你马上就能给他们制定出寿:P)

String text = "lalala @red lalala"; 
     int indexForRed = text.indexOf("@"); 
     int indexForSpace = -1; 
     String redText; 
     String beforeRedText; 
     String afterRedText; 


     while (indexForRed != -1) 
     { 
      indexForSpace = text.indexOf(" ", indexForRed); 
      redText = text.substring(indexForRed + 1, indexForSpace);   
      beforeRedText = indexForRed > 0 ? text.substring(0, indexForRed - 1) : ""; 
      afterRedText = indexForSpace < text.length() - 1 ? text.substring(indexForSpace + 1, text.length()) : ""; 

      text = beforeRedText + "<font color=\"#ff0000\">" + redText + "</font>" + afterRedText;   

      indexForRed = text.indexOf("@", text.indexOf("</font>", indexForSpace)); 
     } 
tv.setText(Html.fromHtml(text)); 

此外,您可以考虑使用不同的分母为其他颜色(而不是#...也许$,%,^),因为#用于在字体中指定颜色,它可能会混淆整个算法。如果你必须使用#,那么一定要在搜索@之前搜索#,它应该可以正常工作。

希望这有助于:)

0

尝试HTML造型:

String data = "<font color=##00FFFF>blue</font> <font color=#FFFF00>yellow</font>"; 

((TextView) vi.findViewById(R.id.textData)) 
         .setText(Html.fromHtml(data)); 

希望这有助于

0

下面是设置动态文本颜色的例子:

SpannableString text = new SpannableString("Lorem ipsum dolor sit amet"); 
// make "Lorem" (characters 0 to 5) red 
text.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0); 
textView.setText(text, BufferType.SPANNABLE); 

使用indexOf(char c)获得'@'和'#'的索引。

0

Html.fromHtml是一个很糟糕的解决方法,你可以使用它,通过形成peoper html标签并从html中调用,但在该方法解析你的html并创建跨度和使用Spannable接口的引擎下,这就是你真正尝试

0
String res1[] = {"#11111","#22222","#3333","#4444","#555","#666","#77","#88","#99","#100","#161","#152"}; 

String aa=(String) (res1[position]+","+res[position]+"---"+des[position]); 


if (res1[position].startsWith("#")) { 
    aa=aa.replaceAll(res1[position], "<b>" + res1[position] + "</b>"); 
} 

holder.problem.setText(Html.fromHtml(AA));

代替加粗我们可以使用ant标签ex:...... ect ,.

相关问题