现在的位置: 首页 > 综合 > 正文

自定义TextView 链接

2014年10月19日 ⁄ 综合 ⁄ 共 2743字 ⁄ 字号 评论关闭
文章目录

android 解析微博内容,自定义TextView 链接

参考
/**
* 解析微博内容的类
* @author boss
*
*/
public class WeiboContentParser {
SpannableString mSpannableString;
private static WeiboContentParser mWeiboContentParser;
public static WeiboContentParser getInstance(){
if(mWeiboContentParser == null){
mWeiboContentParser = new WeiboContentParser();
}
return mWeiboContentParser;
}
private final static String VOICE_TAG = “#
#”;
private final static String SINA_HTTP = “http://t\\.cn/\\w+”;
/**
* 微博内容中的at正则表达式
*/
private final Pattern AT_PATTERN = Pattern.compile(“@[\\u4e00-\\u9fa5\\w\\-]+”);
/**
* 微博内容中的#话题#正则表达式
*/
private final Pattern TAG_PATTERN = Pattern.compile(“#([^\\#|.]+)#”);
/**
* 微博内容中的at正则表达式
*/
private final Pattern VOICE_PATTERN = Pattern.compile(VOICE_TAG+SINA_HTTP);
/**
* 微博内容中的url正则表达式
*/
private final Pattern URL_PATTERN = Pattern.compile(SINA_HTTP);
public SpannableString parse(String input) {
SpannableString mSpannableString = new SpannableString(input);
Matcher mAtMatcher = AT_PATTERN.matcher(input);
while(mAtMatcher.find()){
String atNameMatch = mAtMatcher.group();
String subAtNameMatch = atNameMatch.substring(1, atNameMatch.length());
mSpannableString.setSpan(new WeiboAtClickSpan(subAtNameMatch), mAtMatcher.start(), mAtMatcher.end(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Matcher mTopicMatcher = TAG_PATTERN.matcher(input);
while(mTopicMatcher.find()){
String tagNameMatch = mTopicMatcher.group();
String subTagNameMatch = tagNameMatch.substring(1, tagNameMatch.length()-1);
mSpannableString.setSpan(new WeiboTagClickSpan(subTagNameMatch), mTopicMatcher.start(), mTopicMatcher.end(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Matcher mVoiceMatcher = VOICE_PATTERN.matcher(input);
while(mVoiceMatcher.find()){
String voiceNameMatch = mVoiceMatcher.group();
String subVoiceNameMatch = voiceNameMatch.substring(VOICE_TAG.length(), voiceNameMatch.length());
mSpannableString.setSpan(new BackgroundColorSpan(Color.RED), mVoiceMatcher.start()+VOICE_TAG.length(), mVoiceMatcher.end(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
mSpannableString.setSpan(new WeiboVoiceClickSpan(subVoiceNameMatch), mVoiceMatcher.start()+VOICE_TAG.length(), mVoiceMatcher.end(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
Matcher mUrlMatcher = URL_PATTERN.matcher(input);
while(mUrlMatcher.find()){
String urlNameMatch = mUrlMatcher.group();
mSpannableString.setSpan(new WeiboUrlClickSpan(urlNameMatch), mUrlMatcher.start(), mUrlMatcher.end(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return mSpannableString;
}
}
/**
*WeiboClickSpan类,WeiboAtClickSpan 等类继承自此类,在onClick(View widget, String content)方法中做调用操作
*/
public abstract class WeiboClickSpan extends ClickableSpan {
// 点击的内容
String mContent;
public WeiboClickSpan(String content){
mContent = content;
}
@Override
public void onClick(View widget) {
onClick(widget, mContent);
}
abstract void onClick(View widget, String content);
}
}
【上篇】
【下篇】

抱歉!评论已关闭.