Android 在同一个 TextView 中链接 web 和 @mentions [英] Android Linkify both web and @mentions all in the same TextView

查看:30
本文介绍了Android 在同一个 TextView 中链接 web 和 @mentions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我昨天问了这个问题:

Ok so I asked this yesterday:

在 Twitter 客户端中自动链接 @mentions

我的@mentions 链接正确.但为了让它工作,我不得不将 android:autoLink="web" 从我的 xml 中取出用于 TextView.所以现在我得到了@mentions 的链接,但它不再链接 URL.我尝试像这样进行两个单独的 Linkify.addLinks() 调用:

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);

但是最后被调用的是被应用的那个.谁能告诉我如何让它链接@提及并仍然自动链接 URL?

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?

编辑以澄清更多代码.

推荐答案

这是我链接所有 Twitter 链接(提及、主题标签和 URL)的代码:

Here's my code to linkify all Twitter links (mentions, hashtags and URLs):

TextView tweet = (TextView) findViewById(R.id.tweet);

TransformFilter filter = new TransformFilter() {
    public final String transformUrl(final Matcher match, String url) {
        return match.group();
    }
};

Pattern mentionPattern = Pattern.compile("@([A-Za-z0-9_-]+)");
String mentionScheme = "http://www.twitter.com/";
Linkify.addLinks(tweet, mentionPattern, mentionScheme, null, filter);

Pattern hashtagPattern = Pattern.compile("#([A-Za-z0-9_-]+)");
String hashtagScheme = "http://www.twitter.com/search/";
Linkify.addLinks(tweet, hashtagPattern, hashtagScheme, null, filter);

Pattern urlPattern = Patterns.WEB_URL;
Linkify.addLinks(tweet, urlPattern, null, null, filter);

这篇关于Android 在同一个 TextView 中链接 web 和 @mentions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆