package com.diandi.demo.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.widget.EditText;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* *******************************************************************************
* ********* Author : klob(kloblic@gmail.com) .
* ********* Date : 2014-11-29 .
* ********* Time : 11:46 .
* ********* Project name : Diandi1.18 .
* ********* Version : 1.0
* ********* Copyright @ 2014, klob, All Rights Reserved
* *******************************************************************************
*/
public class EmoticonsEditText extends EditText {
public EmoticonsEditText(Context context) {
super(context);
}
public EmoticonsEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public EmoticonsEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setText(CharSequence text, BufferType type) {
if (!TextUtils.isEmpty(text)) {
super.setText(replace(text.toString()), type);
} else {
super.setText(text, type);
}
}
private Pattern buildPattern() {
return Pattern.compile("\\\\ue[a-z0-9]{3}", Pattern.CASE_INSENSITIVE);
}
private CharSequence replace(String text) {
try {
SpannableString spannableString = new SpannableString(text);
int start = 0;
Pattern pattern = buildPattern();
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
String faceText = matcher.group();
String key = faceText.substring(1);
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(),
getContext().getResources().getIdentifier(key, "drawable", getContext().getPackageName()), options);
ImageSpan imageSpan = new ImageSpan(getContext(), bitmap);
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);
}
return spannableString;
} catch (Exception e) {
return text;
}
}
}