就是飞一样的感觉
1、加依赖, 包很小,几乎不占包体积
//Emoji表情转换成各类可见字符串:https://github.com/vdurmont/emoji-java/
compile'com.vdurmont:emoji-java:+'
2、上传的文本内容,(带emoji表情)
URLEncoder.encode(StringUtil.emoji2Unicode(EmojiParser.parseToUnicode(mEditContent.getText().toString().trim())))
/**
* 将字符串中的Emoji表情转换成Unicode编码
* @param src
* @return
*/
public static String emoji2Unicode(String src) {
StringBuffer unicode = new StringBuffer();
for (int i = 0; i < src.length(); i++) {
char c = src.charAt(i);
int codepoint = src.codePointAt(i);
if(isEmojiCharacter(codepoint)) {
unicode.append("\\u").append(Integer.toHexString(c));
} else {
unicode.append(c);
}
}
return unicode.toString();
}
3、显示的文本(带emoji表情)
StringUtil.unicode2Emoji(item.getTitle())
/**
* 将Unicode字符转成中文
* @param src
* @return
*/
public static String unicode2Emoji(String src) {
if (TextUtils.isEmpty(src)) {
return "";
}
StringBuffer retBuf = new StringBuffer();
int maxLoop = src.length();
for (int i = 0; i < maxLoop; i++) {
if (src.charAt(i) == '\\') {
if ((i < maxLoop - 5) && ((src.charAt(i + 1) == 'u') || (src.charAt(i + 1) == 'U'))) {
try {
retBuf.append((char) Integer.parseInt(src.substring(i + 2, i + 6), 16));
i += 5;
} catch (NumberFormatException localNumberFormatException) {
retBuf.append(src.charAt(i));
}
} else {
retBuf.append(src.charAt(i));
}
} else {
retBuf.append(src.charAt(i));
}
}
return retBuf.toString();
}
网友评论