Linkify Android 转换问题
尝试做一些相当简单的事情。
像这样获取文本
用户名:这是我正在发表的评论
,它位于单个 TextView 中。我想让用户名成为一个链接。我决定最简单的事情就是用“$@”之类的东西包围用户名,这样它就变成了
“$@用户名:$@这是我正在发表的评论
这样我就可以在 Linkify 中使用以下正则表达式
Pattern userName = Pattern.compile(".*\\$@(.+)\\$@.*");
并将其设为链接。但是,显然我需要删除分隔符,因此以下是
title.setText(titleText);
Linkify.TransformFilter transformer = new Linkify.TransformFilter() {
@Override
public String transformUrl(Matcher match, String url) {
return match.group(1);
}
};
Linkify.addLinks(title, userName, "content://user=", null, transformer);
一些 代码然而,由于原因,整个文本变成了一个巨大的链接,并且文本根本没有被转换。
Trying to do something fairly simple.
Taking text like this
User Name: This is a comment I am making
It is in a single TextView. I want to make the User Name a link. I decided that the easiest thing would be to surround the User Name with something like "$@" so it becomes
"$@User Name:$@ This is a comment I am making
That way I can use the following regular expression
Pattern userName = Pattern.compile(".*\\$@(.+)\\$@.*");
with Linkify and make it a link. However, clearly I need to remove the delimiters, so the following is the code
title.setText(titleText);
Linkify.TransformFilter transformer = new Linkify.TransformFilter() {
@Override
public String transformUrl(Matcher match, String url) {
return match.group(1);
}
};
Linkify.addLinks(title, userName, "content://user=", null, transformer);
For some reason however, the whole text becomes one giant link, and the text isn't being transformed at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事实上,事实证明这非常简单。我最终没有使用疯狂的“$@”来分隔用户名,而是坚持使用
所以我最终使用了以下模式
非常简单,代码变得只是
感谢 nil 的原始建议。我确实匹配了整个字符串,而不仅仅是链接的用户名。
It actually did turned out to be pretty easy. I ended up not using the crazy "$@" to delimit the username, instead sticking with just
so I ended up using the following pattern
Very simple, and the code becomes just
Thank you to nil for the original suggestion. I was indeed matching the whole string instead of just the userName which is the link.
我最好的猜测是您使用的正则表达式是问题所在,您告诉它基本上挑选出整个字符串(如果匹配),包括您要查找的内容之前和之后的所有内容。因此,TransformFilter 可能会传递整个匹配的字符串。据我所知,transformUrl 期望您返回 URL,因此整个字符串都链接到第一个匹配组。
因此,考虑到这一点,将正则表达式更改为类似
"\\$@(.+?)\\$@"
(添加 < code>? 在组中使匹配非贪婪),以避免匹配整个字符串并仅挑选出您想要 URL-ize 的位(由于缺乏更好的术语,再加上 -ize言语听起来很酷)。My best guess is the regex you're using is the problem, where you're telling it to basically pick out the entire string if it matches, including everything before and after what you're looking for. So, the
TransformFilter
is probably being passed the entire matched string.transformUrl
as far as I can tell expects you to return the URL, so the entire string is linked to the first match group.So, with that in mind, it's probably in your best interest to change the regex to something along the lines of
"\\$@(.+?)\\$@"
(with an added?
in the group to make the match non-greedy) so as to avoid matching the entire string and just picking out the bit you want to URL-ize (for lack of a better term, plus adding -ize to words sounds cool).为什么不将分隔符放在模式内进行更改?
然后更改转换过滤器以在更改为 URL 时删除开始和结束模式...
Why not put the delimiters inside the pattern to change ?
Then change the transform filter to remove the start and end patterns when changing into the URL...