【问题标题】:How to color text using Regex in android如何在android中使用正则表达式为文本着色
【发布时间】:2013-07-24 19:19:44
【问题描述】:

我有三个正则表达式:

Pattern mentionPattern = Pattern.compile("(@[A-Za-z0-9_-]+)");
Pattern hashtagPattern = Pattern.compile("(#[A-Za-z0-9_-]+)");
Pattern urlPattern = Patterns.WEB_URL;

我有一个字符串:

这是 @tom_cruise#sample #twitter 文本,带有链接 http://tom_cruise.me

我需要将此文本与上述三个正则表达式匹配,并将匹配的文本着色为蓝色,并将最终文本设置为TextView。我怎样才能做到这一点?

需要说明的是,我不需要Linkify文字,只需要着色。而且我没有使用Twitter4j 库。

【问题讨论】:

    标签: java android regex


    【解决方案1】:

    看看SpannableStringSpannableStringBuilderhttps://stackoverflow.com/a/16061128/1321873 提供了使用 SpannableStringBuilder 的示例

    您可以编写一个接受非样式的String 并返回CharSequence 的方法,如下所示:

    private CharSequence getStyledTweet(String tweet){
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(tweet);
        //Find the indices of the hashtag pattern, mention pattern and url patterns 
        //and set the spans accordingly
        //...
        return stringBuilder;
    }
    

    然后使用上面的返回值来设置TextView的文本

    TextView tView = (TextView)findViewById(R.id.myText);
    tView.setText(getStyledTweet(tweet));
    

    【讨论】:

      【解决方案2】:

      我将http://tom_cruise.me 替换为http://www.google.com。请尝试以下操作:

      String a = "This is a #sample #twitter text of @tom_cruise with a link http://www.google.com";
      
      Pattern mentionPattern = Pattern.compile("(@[A-Za-z0-9_-]+)");
      Pattern hashtagPattern = Pattern.compile("(#[A-Za-z0-9_-]+)");
      Pattern urlPattern = Patterns.WEB_URL;
      
      StringBuffer sb = new StringBuffer(a.length());
      Matcher o = hashtagPattern.matcher(a);
      
      while (o.find()) {
          o.appendReplacement(sb, "<font color=\"#437C17\">" + o.group(1) + "</font>");
      }
      o.appendTail(sb);
      
      Matcher n = mentionPattern.matcher(sb.toString());
      sb = new StringBuffer(sb.length());
      
      while (n.find()) {
          n.appendReplacement(sb, "<font color=\"#657383\">" + n.group(1) + "</font>");
      }
      n.appendTail(sb);
      
      Matcher m = urlPattern.matcher(sb.toString());
      sb = new StringBuffer(sb.length());
      
      while (m.find()) {
          m.appendReplacement(sb, "<font color=\"#EDDA74\">" + m.group(1) + "</font>");
      }
      m.appendTail(sb);
      
      textView.setText(Html.fromHtml(sb.toString()));
      

      【讨论】:

      • 感谢您的回答 :) 一切似乎都很好,除了两件事。如何使用颜色代码而不是蓝色?我试过了,但没有用。 '@' 和 '#' 没有显示在最终文本中
      • @typedef 文本的必需部分不是蓝色的?或者你是说你不能使用不同的颜色代码。
      • 我需要一个接近蓝色的不同颜色代码,如#6ccff6,但在应用&lt;font color=\"#6ccff6\"&gt; 后颜色不会改变。相反,它在 textview 中显示标记
      • @typedef 在以骇人听闻的方式解决了颜色代码问题后,我已经编辑了上面的答案。我们在这里所做的是将StringBuffer 从一个Matcher 传递到下一个。所以,Matcher(#[A-Za-z0-9_-]+) 正在搞乱颜色代码。在我上面编辑的代码中,我首先将原始字符串传递给Pattern hashtagPattern。然后,任何出来的东西都会传递给其他匹配器。这解决了您的颜色代码问题。还有,感谢您的接受!
      • 很棒的代码,也检查一下这个是否可以使用 SpannableString stackoverflow.com/a/7837610/3736955
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      • 2018-03-16
      • 2012-05-06
      • 2019-08-16
      • 2010-09-08
      相关资源
      最近更新 更多