处理新浪微博中@ #等格式 蓝色高亮~ 表情 以及点击事件
2013-08-29 14:13
316 查看
先上图看效果
表情只弄了一个,测试使用, 新浪微博接口中返回那么多表情 - - 还没处理,好像新浪微博客户端也有些没有处理到
正则不会处理#的问题
如 : 其他文字#要的#不要的#要的#其他文字
最后会把 不要的 也给匹配到,
我用了笨点的方法处理了。。。
发关键代码,其他的下载附件 = =
TextUtils.java
[java] view
plaincopy
package com.zeng.span2.util;
import java.lang.ref.SoftReference;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.TextView;
import com.zeng.span2.R;
import com.zeng.span2.TestPersonActivity;
import com.zeng.span2.TestTopicActivity;
import com.zeng.span2.model.Emotion;
/**
* 处理字体高亮
*
* @author zeng
*
*/
public class TextUtils {
private static List<Emotion> emotions = new ArrayList<Emotion>();
/**
*
* @param mContext
* @param textview
* @param content
* @param hasClick
* 是否添加click
*/
public static void textViewSpan(Context mContext, TextView textview, String content, boolean hasClick) {
List<PositionItem> list = paresString2(content);
Spannable span = new SpannableString(content);
// 测试表情
Emotion emotion = new Emotion(
"http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/7a/shenshou_org.gif", "[草泥马]");
if (emotions.isEmpty())
emotions.add(emotion);
// 结束测试
for (PositionItem pi : list) {
if (pi.getPrefixType() == 4) {
String imageName = "";
for (Emotion em : emotions) {
if (em.getPhrase().equals(pi.getContent())) {
imageName = em.getSaveName2();
break;
}
}
//
try {
Field f = (Field) R.drawable.class.getDeclaredField(imageName);
int eId = f.getInt(R.drawable.class);
Drawable drawable = mContext.getResources().getDrawable(eId);
if (drawable != null) {
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
ImageSpan imgSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
span.setSpan(imgSpan, pi.start, pi.end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
span.setSpan(new ForegroundColorSpan(Color.BLUE), pi.start, pi.end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
} catch (Exception e) {
// TODO: handle exception
span.setSpan(new ForegroundColorSpan(Color.BLUE), pi.start, pi.end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
} else {
if (hasClick)
span.setSpan(new TextClickSapn(mContext, pi), pi.start, pi.end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
else
span.setSpan(new ForegroundColorSpan(Color.BLUE), pi.start, pi.end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
textview.setText(span);
if (hasClick)
textview.setMovementMethod(LinkMovementMethod.getInstance());
}
public static List<PositionItem> paresString(String content) {
String regex = "@[^\\s::《]+([\\s::《]|$)|#(.+?)#|http://t\\.cn/\\w+|\\[(.*?)\\]";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(content);
boolean b = m.find();
List<PositionItem> list = new ArrayList<PositionItem>();
while (b) {
System.out.println(m.start());
System.out.println(m.end());
System.out.println(m.group());
int start = m.start();
int end = m.end();
String str = m.group();
list.add(new PositionItem(start, end, str, content.length()));
b = m.find(m.end() - 1);
}
return list;
}
/**
* 这个是处理一条信息有多个#...
*
* @param content
* @return
*/
public static List<PositionItem> paresString2(String content) {
String regex = "@[^\\s::《]+([\\s::《]|$)|#(.+?)#|http://t\\.cn/\\w+|\\[(.*?)\\]";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(content);
boolean b = m.find();
List<PositionItem> list = new ArrayList<PositionItem>();
int count = 0;
int lastIndex = 0;
while (b) {
System.out.println(m.start());
System.out.println(m.end());
System.out.println(m.group());
int start = m.start();
int end = m.end();
String str = m.group();
if (str.startsWith("#")) {
count++;
if (count % 2 == 0) {
b = m.find(lastIndex);
continue;
}
}
list.add(new PositionItem(start, end, str, content.length()));
b = m.find(m.end() - 1);
try {
lastIndex = m.start() + 1;
} catch (Exception e) {
// TODO: handle exception
}
}
return list;
}
private static class TextClickSapn extends ClickableSpan {
private PositionItem item;
private Context mContext;
public TextClickSapn(Context context, PositionItem item) {
// TODO Auto-generated constructor stub
this.item = item;
this.mContext = context;
}
@Override
public void onClick(View widget) {
// TODO Auto-generated method stub
switch (item.getPrefixType()) {
case 1:
Intent it_person = new Intent(mContext, TestPersonActivity.class);
it_person.putExtra("content", item.getContentWithoutPrefix());
mContext.startActivity(it_person);
break;
case 2:
Intent it_topic = new Intent(mContext, TestTopicActivity.class);
it_topic.putExtra("content", item.getContentWithoutPrefix());
mContext.startActivity(it_topic);
break;
case 3:
// 直接使用调用浏览器
// 这个是短链 ,还需要条用微博接口,转成原始连接 才能访问
// 先使用短链去调用接口,获取长链,再启动浏览器
Intent intent = new Intent();
// intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse("http://www.sina.com");
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(content_url);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
break;
default:
break;
}
}
}
public static class PositionItem {
public int start;
public int end;
private int prefixType;
private String content;
private int strLenght;
public PositionItem(int start, int end, String content, int strLenght) {
// TODO Auto-generated constructor stub
this.start = start;
this.end = end;
this.content = content;
this.strLenght = strLenght;
}
public PositionItem(int start, int end, String content) {
// TODO Auto-generated constructor stub
this.start = start;
this.end = end;
this.content = content;
}
public String getContent() {
return content;
}
public String getContentWithoutPrefix() {
switch (getPrefixType()) {
case 1:
if (end == strLenght)
return content.substring(1, strLenght);
return content.substring(1, content.length() - 1);
case 2:
return content.substring(1, content.length() - 1);
case 3:
return content;
default:
return content;
}
}
/**
* 1 @ 人物 2 # 话题 3 http://t.cn/ 短链 4 [ 表情
*
* @return
*/
public int getPrefixType() {
if (content.startsWith("@"))
return 1;
if (content.startsWith("#"))
return 2;
if (content.startsWith("http://"))
return 3;
if (content.startsWith("["))
return 4;
return -1;
}
}
}
若有好的办法处理#的问题,求告知!
下面是附件连接
http://download.csdn.net/download/zgf1991/4888699
点击打开链接
表情只弄了一个,测试使用, 新浪微博接口中返回那么多表情 - - 还没处理,好像新浪微博客户端也有些没有处理到
正则不会处理#的问题
如 : 其他文字#要的#不要的#要的#其他文字
最后会把 不要的 也给匹配到,
我用了笨点的方法处理了。。。
发关键代码,其他的下载附件 = =
TextUtils.java
[java] view
plaincopy
package com.zeng.span2.util;
import java.lang.ref.SoftReference;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.TextView;
import com.zeng.span2.R;
import com.zeng.span2.TestPersonActivity;
import com.zeng.span2.TestTopicActivity;
import com.zeng.span2.model.Emotion;
/**
* 处理字体高亮
*
* @author zeng
*
*/
public class TextUtils {
private static List<Emotion> emotions = new ArrayList<Emotion>();
/**
*
* @param mContext
* @param textview
* @param content
* @param hasClick
* 是否添加click
*/
public static void textViewSpan(Context mContext, TextView textview, String content, boolean hasClick) {
List<PositionItem> list = paresString2(content);
Spannable span = new SpannableString(content);
// 测试表情
Emotion emotion = new Emotion(
"http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/7a/shenshou_org.gif", "[草泥马]");
if (emotions.isEmpty())
emotions.add(emotion);
// 结束测试
for (PositionItem pi : list) {
if (pi.getPrefixType() == 4) {
String imageName = "";
for (Emotion em : emotions) {
if (em.getPhrase().equals(pi.getContent())) {
imageName = em.getSaveName2();
break;
}
}
//
try {
Field f = (Field) R.drawable.class.getDeclaredField(imageName);
int eId = f.getInt(R.drawable.class);
Drawable drawable = mContext.getResources().getDrawable(eId);
if (drawable != null) {
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
ImageSpan imgSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
span.setSpan(imgSpan, pi.start, pi.end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
span.setSpan(new ForegroundColorSpan(Color.BLUE), pi.start, pi.end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
} catch (Exception e) {
// TODO: handle exception
span.setSpan(new ForegroundColorSpan(Color.BLUE), pi.start, pi.end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
} else {
if (hasClick)
span.setSpan(new TextClickSapn(mContext, pi), pi.start, pi.end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
else
span.setSpan(new ForegroundColorSpan(Color.BLUE), pi.start, pi.end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
textview.setText(span);
if (hasClick)
textview.setMovementMethod(LinkMovementMethod.getInstance());
}
public static List<PositionItem> paresString(String content) {
String regex = "@[^\\s::《]+([\\s::《]|$)|#(.+?)#|http://t\\.cn/\\w+|\\[(.*?)\\]";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(content);
boolean b = m.find();
List<PositionItem> list = new ArrayList<PositionItem>();
while (b) {
System.out.println(m.start());
System.out.println(m.end());
System.out.println(m.group());
int start = m.start();
int end = m.end();
String str = m.group();
list.add(new PositionItem(start, end, str, content.length()));
b = m.find(m.end() - 1);
}
return list;
}
/**
* 这个是处理一条信息有多个#...
*
* @param content
* @return
*/
public static List<PositionItem> paresString2(String content) {
String regex = "@[^\\s::《]+([\\s::《]|$)|#(.+?)#|http://t\\.cn/\\w+|\\[(.*?)\\]";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(content);
boolean b = m.find();
List<PositionItem> list = new ArrayList<PositionItem>();
int count = 0;
int lastIndex = 0;
while (b) {
System.out.println(m.start());
System.out.println(m.end());
System.out.println(m.group());
int start = m.start();
int end = m.end();
String str = m.group();
if (str.startsWith("#")) {
count++;
if (count % 2 == 0) {
b = m.find(lastIndex);
continue;
}
}
list.add(new PositionItem(start, end, str, content.length()));
b = m.find(m.end() - 1);
try {
lastIndex = m.start() + 1;
} catch (Exception e) {
// TODO: handle exception
}
}
return list;
}
private static class TextClickSapn extends ClickableSpan {
private PositionItem item;
private Context mContext;
public TextClickSapn(Context context, PositionItem item) {
// TODO Auto-generated constructor stub
this.item = item;
this.mContext = context;
}
@Override
public void onClick(View widget) {
// TODO Auto-generated method stub
switch (item.getPrefixType()) {
case 1:
Intent it_person = new Intent(mContext, TestPersonActivity.class);
it_person.putExtra("content", item.getContentWithoutPrefix());
mContext.startActivity(it_person);
break;
case 2:
Intent it_topic = new Intent(mContext, TestTopicActivity.class);
it_topic.putExtra("content", item.getContentWithoutPrefix());
mContext.startActivity(it_topic);
break;
case 3:
// 直接使用调用浏览器
// 这个是短链 ,还需要条用微博接口,转成原始连接 才能访问
// 先使用短链去调用接口,获取长链,再启动浏览器
Intent intent = new Intent();
// intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse("http://www.sina.com");
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(content_url);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
break;
default:
break;
}
}
}
public static class PositionItem {
public int start;
public int end;
private int prefixType;
private String content;
private int strLenght;
public PositionItem(int start, int end, String content, int strLenght) {
// TODO Auto-generated constructor stub
this.start = start;
this.end = end;
this.content = content;
this.strLenght = strLenght;
}
public PositionItem(int start, int end, String content) {
// TODO Auto-generated constructor stub
this.start = start;
this.end = end;
this.content = content;
}
public String getContent() {
return content;
}
public String getContentWithoutPrefix() {
switch (getPrefixType()) {
case 1:
if (end == strLenght)
return content.substring(1, strLenght);
return content.substring(1, content.length() - 1);
case 2:
return content.substring(1, content.length() - 1);
case 3:
return content;
default:
return content;
}
}
/**
* 1 @ 人物 2 # 话题 3 http://t.cn/ 短链 4 [ 表情
*
* @return
*/
public int getPrefixType() {
if (content.startsWith("@"))
return 1;
if (content.startsWith("#"))
return 2;
if (content.startsWith("http://"))
return 3;
if (content.startsWith("["))
return 4;
return -1;
}
}
}
若有好的办法处理#的问题,求告知!
下面是附件连接
http://download.csdn.net/download/zgf1991/4888699
点击打开链接
相关文章推荐
- 处理新浪微博中@ #等格式 蓝色高亮~ 表情 以及点击事件
- 点击事件处理, 以及hitTest:withEvent:实现
- 新浪微博-表情键盘的事件处理
- Android 在ListView嵌套GridView中出现的点击事件冲突的处理以及gridview显示不全的问题
- RecycleView的通用适配器BaseQuickAdapter处理item中子控件的点击事件以及cardview卡片阴影
- checkbox扩大点击范围以及一个监听器处理多个点击事件
- 在ListView嵌套GridView中出现的点击事件冲突的处理以及gridview显示不全的问题
- AWT/Swing中处理鼠标双击(多击)以及右键点击的事件处理办法
- [Android]在ListView嵌套GridView中出现的点击事件处理以及gridview显示不全的有关问题
- 新浪微博里的字体加亮的点击事件处理(原创)
- 处理dataTable 刷新 分页以及行点击事件
- 安卓开发笔记——高仿新浪微博文字处理(实现关键字高亮,自定义表情替换并加入点击事件实现)
- WebView加载网页以及处理里面的超链接点击事件
- 新浪微博里的字体加亮的点击事件处理
- CCMenu超过CCScrollView边框还能点击处理,以及拖动CCMenuItem不响应点击事件
- [Android]在ListView嵌套GridView中出现的点击事件处理以及gridview显示不全的问题
- ListView嵌套ListView涉及到的子ListView无法显示完全,以及父ListView无法执行item的点击事件的处理
- Android事件分发机制以及滑动冲突处理
- HTML点击事件重叠的处理
- 子视图超出父视图部分仍然响应点击事件的处理