android textview和edittext中插入表情

时间:2021-07-21 21:41:14
关键是下面几步
1、在res/values/下创建arrays.xml添加下面定义表情的array,[XX]和[YY]就代表表情字段,可以自己定义,这就是到时候发表表情时真正的东西(代表对应的表情图片)。

[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">

<string-array name="smiley_array">
<item>[XX]</item>
//有几种表情就写几个item......
<item>[YY]</item>
</string-array>

</resources>
2、将自己要用的表情图片放入res/drawable/下
3、将下面工具类拷入自己的工程里,还需要进行稍微修改

[java] view plaincopy
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SmileyParser {
private static SmileyParser sInstance;

public static SmileyParser getInstance() {
return sInstance;
}

public static void init(Context context) {
sInstance = new SmileyParser(context);
}

private final Context mContext;
private final String[] mSmileyTexts;
private final Pattern mPattern;
private final HashMap<String, Integer> mSmileyToRes;

private SmileyParser(Context context) {
mContext = context;
mSmileyTexts = mContext.getResources().getStringArray(
DEFAULT_SMILEY_TEXTS);
mSmileyToRes = buildSmileyToRes();
mPattern = buildPattern();
}

static class Smileys {
// 表情图片集合
private static final int[] sIconIds = { R.drawable.image0,
R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4};
// 将图片映射为 文字
public static int dk = 0;
public static int dx = 1;
public static int hx = 2;
public static int hy = 3;
public static int jy = 4;

// 得到图片表情 根据id
public static int getSmileyResource(int which) {
return sIconIds[which];
}
}

public static final int[] DEFAULT_SMILEY_RES_IDS = {
Smileys.getSmileyResource(Smileys.dk), // 0
Smileys.getSmileyResource(Smileys.dx), // 1
Smileys.getSmileyResource(Smileys.hx), // 2
Smileys.getSmileyResource(Smileys.hy), // 3
Smileys.getSmileyResource(Smileys.jy), // 4
};

public static final int DEFAULT_SMILEY_TEXTS = R.array.smiley_array;
private HashMap<String, Integer> buildSmileyToRes() {
if (DEFAULT_SMILEY_RES_IDS.length != mSmileyTexts.length) {
throw new IllegalStateException("Smiley resource ID/text mismatch");
}
HashMap<String, Integer> smileyToRes = new HashMap<String, Integer>(
mSmileyTexts.length);
for (int i = 0; i < mSmileyTexts.length; i++) {
smileyToRes.put(mSmileyTexts[i], DEFAULT_SMILEY_RES_IDS[i]);
}
return smileyToRes;
}

// 构建正则表达式
private Pattern buildPattern() {
StringBuilder patternString = new StringBuilder(mSmileyTexts.length * 3);
patternString.append('(');
for (String s : mSmileyTexts) {
patternString.append(Pattern.quote(s));
patternString.append('|');
}
patternString.replace(patternString.length() - 1, patternString
.length(), ")");
return Pattern.compile(patternString.toString());
}

// 根据文本替换成图片
public CharSequence strToSmiley(CharSequence text) {
SpannableStringBuilder builder = new SpannableStringBuilder(text);
Matcher matcher = mPattern.matcher(text);
while (matcher.find()) {
int resId = mSmileyToRes.get(matcher.group());
Drawable drawable = mContext.getResources().getDrawable(resId);
drawable.setBounds(0, 0, 25, 25);//这里设置图片的大小
ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM);
builder.setSpan(imageSpan, matcher.start(),
matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return builder;
}
}

4、最后就是怎么使用了
(1)初始化
[java] view plaincopy
SmileyParser.init(this);
smileyParser = SmileyParser.getInstance();//进行初始化

(2)最后的使用
[java] view plaincopy
//faceStr代表表情字段,例如[XX]
String newContent = myEditText.getText().toString() + faceStr;
CharSequence replace = smileyParser.strToSmiley(newContent);
myEditText.setText(replace);
myEditText.setSelection(replace.length());

5、这样就可以看到自己的myEditText已经插入了对应的表情图片