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

问题描述

好了,所以我问这个昨天:

Ok so I asked this yesterday:

自动链接的Twitter客户端 @mentions

AutoLink @mentions in a twitter client

我得到了我的@mentions正确连接。但是,为了得到它的工作,我不得不采取的android:自动链接=网站我的XML的TextView的。所以,现在我得到的链接@mentions,但它不再链接的网址。我试图做两个独立的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);

但它曾经被称为最后是被应用之一。谁能告诉我怎么可以把它连接两个@mentions仍然自动链接的网址吗?

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?

编辑,以澄清一些更多的code。

Edited to clarify some more of the code.

推荐答案

下面是我的code到linkify所有的微博链接(提到,井号标签和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的Linkify Web和@mentions都在同一个TextView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 17:26