TextView部分文字高亮显示并同时显示表情

本文介绍了一种处理文本视图的方法,包括链接解析、话题标记和表情符号的显示。通过对文本进行分析,实现自动链接跳转及表情替换等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里显示表情的思路是,先把表情准备到本地中,给表情添加一个bean,TextView通过替换编码来显示表情。

 

例如【嘻嘻】,找到这样的字符串则去本地文件夹中寻找对应的图片并显示。

 

package com.uim.microblog.util;

import java.lang.reflect.Field;
import java.util.List;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.text.style.URLSpan;
import android.util.Log;
import android.widget.TextView;

import com.uim.R;
import com.uim.microblog.model.Emotions;
import com.uim.microblog.ui.BlogHomeActivity;

/**
 * 处理TextView界面的类
 * @author mzba
 * @version 2011-06-23
 *
 */
public class TextAutoLink {

	// 加入话题 好友 URL的连结
	public static char strarray[];

	public static void addURLSpan(Activity activity, String str, TextView textView) {
		try{
			SpannableString ss = new SpannableString(str);
			strarray = str.toCharArray();
			int l = str.length() - 10;
			for (int i = 0; i < l; i++) {
				if (strarray[i] == 'h' && strarray[i + 1] == 't'
						&& strarray[i + 2] == 't' && strarray[i + 3] == 'p'
						&& strarray[i + 4] == ':' && strarray[i + 5] == '/'
						&& strarray[i + 6] == '/') {
					StringBuffer sb = new StringBuffer("http://");
					for (int j = i + 7; true; j++) {
						if (strarray[j] != ' ')
							sb.append(strarray[j]);
						else {
							Log.d("http", sb.toString());
							ss.setSpan(new URLSpan(sb.toString()), i, j,
									Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
							i = j;
							break;
						}
					}
				}
			}
			// 处理话题
			l = str.length();
			StringBuffer sb = null;
			boolean start = false;
			int startIndex = 0;
			for (int i = 0; i < l; i++) {
				if (strarray[i] == '@') {
					start = true;
					sb = new StringBuffer("weibo://weibo.view/");
					startIndex = i;
				} else {
					if (start) {
						if (strarray[i] == ':') {
							ss.setSpan(new URLSpan(sb.toString()), startIndex, i,
									Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
							sb = null;
							start = false;
						} else {
							sb.append(strarray[i]);
						}
					}
				}

			}
			// 处理 话题
			start = false;
			startIndex = 0;
			for (int i = 0; i < l; i++) {
				if (strarray[i] == '#') {
					if (!start) {
						start = true;
						sb = new StringBuffer("weibo://weibo.view/");
						startIndex = i;
					} else {
						sb.append('#');
						ss.setSpan(new URLSpan(sb.toString()), startIndex, i + 1,
								Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
						sb = null;
						start = false;
					}
				} else {
					if (start) {
						sb.append(strarray[i]);
					}
				}
			}
			
                        //处理显示表情
			String content = str;
			int len = 0;
			int starts = 0;
			int end = 0;
			while(len < content.length()){
				if(content.indexOf("[", starts) != -1 && content.indexOf("]", end) != -1){
					starts = content.indexOf("[", starts);
					end = content.indexOf("]", end);
					String phrase = content.substring(starts,end + 1);
					String imageName = "";
					List<Emotions> list = BlogHomeActivity.emotions;
					for (Emotions emotions : list) {
						if (emotions.getPhrase().equals(phrase)) {
							imageName = emotions.getImageName();
						}
					}
					
					try {
						Field f = (Field)R.drawable.class.getDeclaredField(imageName);
						int i= f.getInt(R.drawable.class);
						Drawable drawable = activity.getResources().getDrawable(i);  
						if (drawable != null) {
							drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); 
					        ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);  
					        ss.setSpan(span, starts,end + 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);  
						}
					} catch (SecurityException e) {
						e.printStackTrace();
					} catch (NoSuchFieldException e) {
						e.printStackTrace();
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						
					}
					starts = end;
					len = end;
					end++;
				}else{
					starts++;
					end++;
					len = end;
				}
			}
			
			textView.setText(ss); // 设定TextView话题和url和好友 连接
			strarray = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值