EditText中包含表情符号

2015/09/11   
分类:  blog    android   

聊天表情越来越丰富了,整理一下表情图片与EditText的结合(TextView同理)

先上个图:

===========

1、通过ImageSpan将图片嵌入到文本中

Drawable drawable;//图片
...
//让表情符号跟文字的大小一致
drawable.setBounds(0, 0, editText.getLineHeight(), editText.getLineHeight());
ImageSpan imageSpan = new ImageSpan(drawable);//也可以用Bitmap创建

//{:1:}对应一个表情符号
SpannableString spannableString = new SpannableString("{:1:}haha");
//0,5是{:1:},在原来文本的位置
spannableString.setSpan(imageSpan, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

editText.setText(spannableString);

2、利用反射去获取图片

//key是图片名字,在drawable中,app package
getContext().getResources().getIdentifier(key, "drawable", getContext().getPackageName())

3、正则表达式来替换表情符号

private CharSequence replace(String text) {
    SpannableString spannableString = new SpannableString(text);
    int start = 0;
    //{:n:} 1-2数字
    Pattern pattern = Pattern patternPattern.compile("\\{:\\d{1,2}:\\}", Pattern.CASE_INSENSITIVE);

    Matcher matcher = pattern.matcher(text);
    //遍历
    while (matcher.find()) {
        try{
            //获取对应的表情字段{:n:}中的n
    	String faceText = matcher.group();
    	String key = faceText.substring(2, faceText.lastIndexOf(":"));
    	key = String.format("face_%03d", Integer.parseInt(key));

    	//利用反射获取图片		
    	Drawable drawable = getContext().getResources().getDrawable( getContext().getResources().getIdentifier(key, "drawable", getContext().getPackageName()) );
    	drawable.setBounds(0, 0, getLineHeight(), getLineHeight());
    	ImageSpan imageSpan = new ImageSpan(drawable);
    	//防止找到重复的				
    	int startIndex = text.indexOf(faceText, start);
    	int endIndex = startIndex + faceText.length();
    	if (startIndex >= 0)
    	    spannableString.setSpan(imageSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    	start = (endIndex - 1);
        }catch(Exception e){
    	e.printStackTrace();
        }
    }
}

4、自定义EditText,让表情默默的显示

public class EmoticonsEditText extends EditText {

	...

	@Override
	public void setText(CharSequence text, BufferType type) {
		if (!TextUtils.isEmpty(text)) {
			super.setText(replace(text.toString()), type);
		} else {
			super.setText(text, type);
		}
	}

	private CharSequence replace(String text) {...}
}

5、示意图的表情面板

(稍后再写一篇


本文地址 http://www.0kai.net/blog/2015/09/11/41-edittext-with-emoji.html,转载请注明!