新浪微博里的字体加亮的点击事件处理
2012-12-11 13:37
363 查看
/**
* 将text中@某人、#某主题、http://网址的字体加亮,匹配的表情文字以表情显示
* @param text
* @param context
* @return
* @author lvqiyong
*/
public static SpannableString formatContent(CharSequence text,Context context) {
SpannableString spannableString = new SpannableString(text);
/*
* @[^\\s::]+[::\\s] 匹配@某人 \\[[^0-9]{1,4}\\] 匹配表情
* #([^\\#|.]+)# 匹配#某主题 http://t\\.cn/\\w+ 匹配网址
*/
Pattern pattern = Pattern.compile("@[^\\s::]+[::\\s]|#([^\\#|.]+)#|http://t\\.cn/\\w+|\\[[^0-9]{1,4}\\]");
Matcher matcher = pattern.matcher(spannableString);
final Context mcontext = context;
while (matcher.find()) {
final String match=matcher.group();
if(match.startsWith("@")){ //@某人,加亮字体
spannableString.setSpan(new ClickableSpan()
{
// 在onClick方法中可以编写单击链接时要执行的动作
@Override
public void onClick(View widget)
{
String username = match;
username = username.replace("@", "");
username = username.replace(":", "");
username = username.trim();
Intent intent = new Intent(mcontext,UserInfoActivity.class);
intent.putExtra("userid", username);
mcontext.startActivity(intent);//跳转到用户信息界面
}
}, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new ForegroundColorSpan(0xff0077ff),
matcher.start(), matcher.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else if(match.startsWith("#")){ //#某主题
spannableString.setSpan(new ClickableSpan()
{
// 在onClick方法中可以编写单击链接时要执行的动作
@Override
public void onClick(View widget)
{
String theme = match;
theme = theme.replace("#", "");
theme = theme.trim();
Intent intent = new Intent(mcontext,WeiboList.class);
Bundle bundle=new Bundle();
bundle.putInt(WeiboList.WEIBO_CATE, WeiboList.CATE_THEME);
bundle.putString(WeiboList.THEME, theme);
intent.putExtras(bundle);
mcontext.startActivity(intent);//跳转到话题信息界面
}
}, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new ForegroundColorSpan(0xff0077ff),
matcher.start(), matcher.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else if(match.startsWith("http://")){ //匹配网址
spannableString.setSpan(new ClickableSpan()
{
// 在onClick方法中可以编写单击链接时要执行的动作
@Override
public void onClick(View widget)
{
Uri uri = Uri.parse(match);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
mcontext.startActivity(intent);
}
}, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new ForegroundColorSpan(0xff0077ff),
matcher.start(), matcher.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else if(match.startsWith("[")){ //表情
}
}
return spannableString;
}
From:http://nbacheng.iteye.com/blog/1278771
* 将text中@某人、#某主题、http://网址的字体加亮,匹配的表情文字以表情显示
* @param text
* @param context
* @return
* @author lvqiyong
*/
public static SpannableString formatContent(CharSequence text,Context context) {
SpannableString spannableString = new SpannableString(text);
/*
* @[^\\s::]+[::\\s] 匹配@某人 \\[[^0-9]{1,4}\\] 匹配表情
* #([^\\#|.]+)# 匹配#某主题 http://t\\.cn/\\w+ 匹配网址
*/
Pattern pattern = Pattern.compile("@[^\\s::]+[::\\s]|#([^\\#|.]+)#|http://t\\.cn/\\w+|\\[[^0-9]{1,4}\\]");
Matcher matcher = pattern.matcher(spannableString);
final Context mcontext = context;
while (matcher.find()) {
final String match=matcher.group();
if(match.startsWith("@")){ //@某人,加亮字体
spannableString.setSpan(new ClickableSpan()
{
// 在onClick方法中可以编写单击链接时要执行的动作
@Override
public void onClick(View widget)
{
String username = match;
username = username.replace("@", "");
username = username.replace(":", "");
username = username.trim();
Intent intent = new Intent(mcontext,UserInfoActivity.class);
intent.putExtra("userid", username);
mcontext.startActivity(intent);//跳转到用户信息界面
}
}, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new ForegroundColorSpan(0xff0077ff),
matcher.start(), matcher.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else if(match.startsWith("#")){ //#某主题
spannableString.setSpan(new ClickableSpan()
{
// 在onClick方法中可以编写单击链接时要执行的动作
@Override
public void onClick(View widget)
{
String theme = match;
theme = theme.replace("#", "");
theme = theme.trim();
Intent intent = new Intent(mcontext,WeiboList.class);
Bundle bundle=new Bundle();
bundle.putInt(WeiboList.WEIBO_CATE, WeiboList.CATE_THEME);
bundle.putString(WeiboList.THEME, theme);
intent.putExtras(bundle);
mcontext.startActivity(intent);//跳转到话题信息界面
}
}, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new ForegroundColorSpan(0xff0077ff),
matcher.start(), matcher.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else if(match.startsWith("http://")){ //匹配网址
spannableString.setSpan(new ClickableSpan()
{
// 在onClick方法中可以编写单击链接时要执行的动作
@Override
public void onClick(View widget)
{
Uri uri = Uri.parse(match);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
mcontext.startActivity(intent);
}
}, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new ForegroundColorSpan(0xff0077ff),
matcher.start(), matcher.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else if(match.startsWith("[")){ //表情
}
}
return spannableString;
}
From:http://nbacheng.iteye.com/blog/1278771
相关文章推荐
- 新浪微博里的字体加亮的点击事件处理(原创)
- TextView中的部分文字自定义颜色和点击事件(类似新浪微博带颜色的可点击字体)
- 处理新浪微博中@ #等格式 蓝色高亮~ 表情 以及点击事件
- 处理新浪微博中@ #等格式 蓝色高亮~ 表情 以及点击事件
- 从零开始学Android(二):利用“打招呼”来说明按钮点击事件的三种处理方式
- DrawerLayout的使用 侧滑加载Listview,首页使用Fragment和处理点击触摸冲突、穿透事件和监听菜单滑动状态的改变的详细介绍
- Qt 处理鼠标点击事件
- QML中tableview处理点击事件同时能保证选中行高亮和文字变化
- 在Controller中处理HTML超链接点击事件
- 安卓基础开发两种点击事件处理方式
- Android 系列 6.15处理长时间点击事件
- ListView的item点击事件及item中子view的点击事件冲突处理方法和思路
- iOS中超出父视图的按钮点击事件响应处理
- Androidx学习笔记(4)-- 事件处理的四种写法(点击事件为例)
- android abslistview item点击事件无响应的处理方式
- android 点击、滑动事件的处理 GestureDetector
- Qt for Mac点击dock事件处理
- NGUI5种点击事件处理,和点透处理
- Ext-实用工具 处理点击事件 Ext.util.ClickRepeater
- android textview 如何实现像新浪微博@用户的点击事件