Android循环通过字符串来为特定单词着色

时间:2013-11-25 12:12:37

标签: android string

我有一个字符串,其中包含我想要着色的特定单词。这些单词是以#开头的单词。

    if(title.contains("#")){

        SpannableString WordtoSpan = new SpannableString(title);   

        int idx = title.indexOf("#");
        if (idx >= 0) {

            Pattern pattern = Pattern.compile("[ ,\\.\\n]");
            Matcher matcher = pattern.matcher(title);
            int wordEnd = matcher.find(idx)? matcher.start() : -1;

            if (wordEnd < 0) {
                wordEnd = title.length();
            }
            WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE),
                        idx,
                        wordEnd,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }           

        holder.txtTitle.setText(WordtoSpan);
    } else {
        holder.txtTitle.setText(title);
    }

现在让我们以此字符串为例 粗体部分例如是彩色单词。

  

我喜欢奶酪 #burgers ,以及#ketchup。

^这是我目前的代码。我想要的是为每个#单词着色,而不仅仅是第一个单词。

ex。这就像

  

我喜欢奶酪 #burgers ,以及 #ketchup

我想我需要循环?但是无法清楚地了解并使用x.x


更新 最新的尝试。列表变为空白。

    SpannableString WordtoSpan = new SpannableString(title);

    int end = 0;

    Pattern pattern = Pattern.compile("[ ,\\.\\n]");
    Matcher matcher = pattern.matcher(title);

    int i = title.indexOf("#");

    for (i = 0; i < title.length(); i++) {
    if (i >= 0) {

        int wordEnd = matcher.find(i)? matcher.start() : -1;

        if (wordEnd < 0) {
            wordEnd = title.length();
        }
        WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE),
                    i,
                    wordEnd,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        i = end;
    }    

    holder.txtTitle.setText(WordtoSpan);
    }

5 个答案:

答案 0 :(得分:5)

我之前在这里提供了这个答案 - https://stackoverflow.com/a/20179879/3025732

String text = "I love chicken #burger , cuz they are #delicious";
//get the text from TextView

Pattern HASHTAG_PATTERN = Pattern.compile("#(\\w+|\\W+)");
Matcher mat = HASHTAG_PATTERN.matcher(text);

while (mat.find()) {
  String tag = mat.group(0);

  //String tag will contain the hashtag
  //do operations with the hashtag (change color)
}

[编辑]

我已修改代码并根据您的需要完成:

SpannableString hashText = new SpannableString(text.getText().toString());
Matcher matcher = Pattern.compile("#([A-Za-z0-9_-]+)").matcher(hashText);
while (matcher.find())
{
     hashText.setSpan(new ForegroundColorSpan(Color.BLUE), matcher.start(), matcher.end(), 0);
}
text.setText(hashText);

以下是它的截图:

enter image description here

答案 1 :(得分:4)

我很惊讶没人提到这个:

// String you want to perform on
String toChange = "I love cheese #burgers, and with #ketchup.";

// Matches all characters, numbers, underscores and dashes followed by '#'
// Does not match '#' followed by space or any other non word characters
toChange = toChange.replaceAll("(#[A-Za-z0-9_-]+)", 
                         "<font color='#0000ff'>" + "$0" +  "</font>");

// Encloses the matched characters with html font tags

// Html#fromHtml(String) returns a Spanned object
holder.txtTitle.setText(Html.fromHtml(toChange));

#0000ff ==&gt;&gt; Color.BLUE

截图:

enter image description here

答案 2 :(得分:1)

你需要循环你的比赛。您可以在此处查看以前的帖子:

Finding Multiple Integers Inside A String Using Regex

答案 3 :(得分:1)

检查以下示例:

String title = "Your #complete sentence #testing #test.";
printUsingNormal(title);
printUsingRegex(title);

private static void printUsingRegex(String title) {
    Pattern pattern = Pattern.compile("[ ,\\.\\n]");

    int start = 0;
    int end = 0;
    Matcher matcher = pattern.matcher(title);
    while (end >= 0 && end < title.length()) {
        start = title.indexOf('#', start);
        if (start > 0) {
            end = matcher.find(start) ? matcher.start() : -1;
            if (end > 0) {
                System.out.println("Word : " + title.substring(start, end)
                        + " Start : " + start + " End : " + end);
                start = end;
            }
        } else {
            break;
        }
    }
}

private static void printUsingNormal(String title) {
    int start = 0;
    for (int i = 0; i < title.length(); i++) {
        char c = title.charAt(i);
        if (c == '#') {
            start = i; // Got the starting hash
            int end = title.indexOf(' ', i + 1); // Finding the next space.
            if (end > 0) {
                System.out.println("Word : " + title.substring(start, end)
                        + " Start : " + start + " End : " + end);
                i = end;
            } else {
                end=title.length()-1;
                System.out.println("Word : " + title.substring(start, end)
                        + " Start : " + start + " End : " + end);
                i = end;
            }
        }
    }
}

答案 4 :(得分:0)

使用while()this is the snippet设置在android native messenger中搜索的文本的背景颜色,您可以在代码中尝试相同的操作。