处理新浪微博中@ #等格式 蓝色高亮~ 表情 以及点击事件

先上图看效果

  

    




 表情只弄了一个,测试使用, 新浪微博接口中返回那么多表情 - -  还没处理,好像新浪微博客户端也有些没有处理到
  正则不会处理#的问题
如 : 其他文字#要的#不要的#要的#其他文字
最后会把  不要的  也给匹配到, 

 我用了笨点的方法处理了。。。

发关键代码,其他的下载附件 = = 

 TextUtils.java

 

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

 点击打开链接


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值