【发布时间】:2015-12-31 05:06:24
【问题描述】:
我正在尝试识别我的 TextView 中的主题标签并使其可点击,以便当用户单击该主题标签时我可以将他们带到另一个视图。
我设法使用模式匹配在 TextView 中识别 Hashtags,它们在运行时显示为彩色。但是,我需要使 Hashtag 可点击。
这是我的代码:
SpannableString hashText = new SpannableString("I just watched #StarWars and it was incredible. It's a #MustWatch #StarWars");
Matcher matcher = Pattern.compile("#([A-Za-z0-9_-]+)").matcher(hashText);
while (matcher.find())
{
hashText.setSpan(new ForegroundColorSpan(Color.parseColor("#000763")), matcher.start(), matcher.end(), 0);
String tag = matcher.group(0);
}
holder.caption.setText(hashText);
//I need to set an OnClick listener to all the Hashtags recognised
使用上述相同的解决方案,如何将 onclick 侦听器添加到每个主题标签?
【问题讨论】:
-
有图书馆可以做同样的事情,看看这个:github.com/Danylo2006/HashTagHelper
-
@SachinRao 看起来该库不适用于最低 sdk 9 :(
-
尝试将 2 个 java 文件粘贴到您的任何包中。 (HashTagHelper 和 ClickableForegroundColorSpan)
标签: java android android-activity mobile onclicklistener