Android Linkify web 和 @mentions 都在同一个 TextView 中
好吧,我昨天问了这个:
AutoLink @mentions in a twitter client
我得到了我的@mentions 链接正确。但为了让它工作,我必须将 android:autoLink="web" 从 TextView 的 xml 中取出。所以现在我得到了 @mentions 的链接,但它不再链接 URL。我尝试执行两个单独的 Linkify.addLinks() 调用,如下所示:
mentionFilter = new TransformFilter() {
public final String transformUrl(final Matcher match, String url) {
return match.group(1);
}
};
// Match @mentions and capture just the username portion of the text.
//pattern = Pattern.compile("@([A-Za-z0-9_-]+)");
pattern = Pattern.compile("(@[a-zA-Z0-9_]+)");
scheme = "http://twitter.com/";
tweetTxt = (TextView) v.findViewById(R.id.tweetTxt);
Linkify.addLinks(tweetTxt, pattern, scheme, null, mentionFilter);
Linkify.addLinks(tweetTxt, Linkify.WEB_URLS);
但最后调用的那个调用就是应用的那个调用。谁能告诉我如何让它链接 @mentions 并仍然自动链接 URL?
编辑以澄清更多代码。
Ok so I asked this yesterday:
AutoLink @mentions in a twitter client
I got my @mentions linking correctly. But in order to get it to work I had to take android:autoLink="web" out my xml for the TextView. So now I get links to @mentions but it no longer links URLs. I tried doing two seperate Linkify.addLinks() calls like this:
mentionFilter = new TransformFilter() {
public final String transformUrl(final Matcher match, String url) {
return match.group(1);
}
};
// Match @mentions and capture just the username portion of the text.
//pattern = Pattern.compile("@([A-Za-z0-9_-]+)");
pattern = Pattern.compile("(@[a-zA-Z0-9_]+)");
scheme = "http://twitter.com/";
tweetTxt = (TextView) v.findViewById(R.id.tweetTxt);
Linkify.addLinks(tweetTxt, pattern, scheme, null, mentionFilter);
Linkify.addLinks(tweetTxt, Linkify.WEB_URLS);
But which ever gets called last is the one that gets applied. Can anyone tell me how I can make it link both the @mentions and still autoLink the URLs?
Edited to clarify some more of the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是我的代码,用于链接所有 Twitter 链接(提及、主题标签和 URL):
Here's my code to linkify all Twitter links (mentions, hashtags and URLs):
在 Linkify-ing @ 提及之前,我必须先 Linkify.ALL 才能正常工作。
另外,请勿在 XML 布局文件的 TextView 中使用“android:autoLink”
。我的 TextView 如下所示:
I had to Linkify.ALL before Linkify-ing the @ mentions for it to work.
Also, do not use 'android:autoLink' in the TextView in your XML layout file
My TextView looks like this:
好的,终于有时间将 @mention 和 #hashtags 正确放入 Linkify 类中。而不是仅仅覆盖一些其他类型的链接来使其正常工作。
这个类的工作方式就像普通的 Linkify,但也可以做这两件事。
Ok finally got some time to properly put in the @mention and #hashtags to a Linkify class. Instead of just overriding some of the other types of links to get it working.
This class works just like the normal Linkify, but also can do those two things.
我想出了一个方法来完成这项工作。这有点hacky,但它完成了工作。如果有人知道更合适的方法,请告诉我。
为了让它工作,我给自己制作了 Linkify 类的副本,并编辑了处理电话号码链接的部分以执行 @mention 链接。
这是我找到的地方我的 linkify 类副本
我将其更改
为:
我将此类命名为 MyLinkify 并在我的 Activity 中使用此代码来应用链接。
为了构建 MyLinkify 类,我还必须将 Regex 类的副本添加到我的项目中,这是我找到 Regex.java 的地方
我将其作为答案,以防其他寻找此效果的人找到此线程。我意识到这可能不是让它发挥作用的最佳方法。如果有人知道更好的方法来让它工作,请在此处添加它,我将选择它作为这个问题的实际答案。
I figured out a way to make this work. It is a little bit hacky but it got the job done. If anyone knows of a more appropriate way to do this please do let me know.
To get it working I made myself a copy of the Linkify class and edited the part that handles the linkifying for phone numbers to do @mention links instead.
Here is where I found my copy of the linkify class
I changed this:
into this:
I called this class MyLinkify and used this code in my Activity to apply the links.
To get the MyLinkify class to build I also had to add a copy of the Regex class into my project, here is where I found Regex.java
Im putting this as an answer incase anyone else looking for this effect finds this thread. I realized this is probably not the best way to go about getting this to work. If anyone knows a better way to get it working please add it here and I'll select it is the actual answer to this question.
使用下面的代码:
use below code:
tweetTxt 是您要链接的输入字符串还是 TextView 对象的句柄?
如果它是输入字符串,那么您只是用最终调用 addLinks 的输出覆盖 TextView。根据您的观察,我猜情况确实如此。
在这两种情况下,您都需要将 TextView 传递给 addLinks。在内部,Linkify 将从 TextView 中提取 Spanned 缓冲区并使用它,以便累积对 addLinks 的多次调用。 WikiNotes 示例传递了 TextView,您也应该这样做:http://developer.android。 com/resources/articles/wikinotes-linkify.html
Is tweetTxt the input String that you are linkifying or a handle to the TextView object?
If its the input string, then you are just overwriting the TextView with the output of the final call to addLinks. Given your observation, I'm guessing this is the case.
You need to pass the TextView to addLinks in both cases. Internally, Linkify will pluck the Spanned buffer from the TextView and use that, such that multiple calls to addLinks are cumulative. The WikiNotes example passes the TextView and so should you: http://developer.android.com/resources/articles/wikinotes-linkify.html