安卓使用emoji表情有两种方式
1.得到字符串,转成系统识别的的unicode编码
String s = new String(Character.toChars(integer));
匹配系统的时候,选择表情之后表情不是字符串,提交到后台乱码,暂时没找怎么输入系统表情直接变成字符串
2.在本地一套表情库,通过正则表达式匹配
SpannableString spannStr = new SpannableString(str);
//正则匹配 Pattern p = Pattern.compile("&#[0-9]{6}[;]"); Matcher m = p.matcher(str); int index = 0; // 找到字符串所在的起始位置 Bitmap bm; Bitmap bmp = null; String finds = ""; while (m.find()) { String find = m.group(); Pattern p1 = Pattern.compile(find); Matcher m1 = p1.matcher(finds); int number = 0; while (m1.find()) { number++; } int count = 0; for (int i = 0; i < number; i++) { count = str.indexOf(find, count) + find.length() - 1; } index = str.indexOf(find, count); finds = finds + find; // 如果能找到对应的表情 if (EmojiUtils.sEmojisMap.get(find) != null) { // 根据名字 找到图片 int img = EmojiUtils.sEmojisMap.get(find); bm = BitmapFactory.decodeResource(activity.getResources(), img); } else { bm = BitmapFactory.decodeResource(activity.getResources(), R.drawable.img_emoji); // 图片添加到文字中 ImageSpan span = new ImageSpan(bmp); spannStr.setSpan(span, index, index + find.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); }