1

是否可以从字符串中找到#hashtag 和“http://”链接并为其着色?我在 Android 中使用它。

public void setTitle(String title) {
    this.title = title;
}

提前致谢

4

5 回答 5

11

我找到了答案,这是解决方法:

        SpannableString hashtagintitle = new SpannableString(imageAndTexts1.get(position).getTitle());
        Matcher matcher = Pattern.compile("#([A-Za-z0-9_-]+)").matcher(hashtagintitle);
        while (matcher.find())
        {
            hashtagintitle.setSpan(new ForegroundColorSpan(Color.BLUE), matcher.start(), matcher.end(), 0);

        }
        textView.setText(hashtagintitle);
于 2013-07-30T05:04:27.577 回答
3

如果你想把COMPOUND WORD分成几个部分。

#hashtag#hashtag2#hashtag3

{标签,标签2,标签3}

您可以在下面使用此代码。

public static List<String> getHashTags(String str) {
        Pattern MY_PATTERN = Pattern.compile("(#[a-zA-Z0-9ğüşöçıİĞÜŞÖÇ]{2,50}\\b)");
        Matcher mat = MY_PATTERN.matcher(str);
        List<String> strs = new ArrayList<>();
        while (mat.find()) {
            strs.add(mat.group(1));
        }
        return strs;
    }
于 2019-10-17T07:23:08.073 回答
0

此函数将返回字符串中所有#hashtags 的列表

public static List<String> getHashTags(String str) {
    Pattern MY_PATTERN = Pattern.compile("#(\\S+)");
    Matcher mat = MY_PATTERN.matcher(str);
    List<String> strs = new ArrayList<>();
    while (mat.find()) {
        strs.add(mat.group(1));
    }
    return strs;
}
于 2016-10-10T05:36:45.640 回答
-1

您可以使用Linkify类做的不仅仅是主题标签和网页。是一个关于如何在 textview 中找到主题标签并将其链接到另一个活动的示例。

于 2013-10-05T17:16:03.150 回答
-2

您可以使用正则表达式匹配它。

这是 Android 中使用正则表达式的类。

我很快就找到了这篇关于匹配主题标签的文章

这是一个匹配 URL 的 SO 问题。

于 2013-07-29T16:10:19.300 回答