当我从edittext获取笑脸时,需要将其Unicode发送到服务器上。我使用下面的代码获得了Unicode:
public static String escapeJavaString(String st)
{
StringBuilder builder = new StringBuilder();
try {
for (int i = 0; i < st.length(); i++) {
char c = st.charAt(i);
if(!Character.isLetterOrDigit(c) && !Character.isSpaceChar(c)&& !Character.isWhitespace(c) ){
String unicode = String.valueOf(c);
int code = (int)c;
if(!(code >= 0 && code <= 255)){
unicode = "\\\\u"+Integer.toHexString(c);
}
builder.append(unicode);
}
else{
builder.append(c);
}
}
Log.i("Unicode Block", builder.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return builder.toString();
}
我输出为 '\ud83d\ude12'
,然后将其发送到服务器。在接收方面,我从服务器获得相同但是当它放在EditText中时,它将以与上面输出相同的文本形式显示Unicode,不会获得表情符号。如何解码它以在EditText中显示表情符号而不是Unicode?
任何人,有个主意吗?
答案 0 :(得分:2)
我得到了上述问题的解决方案。调用以下方法作为message.setText(unescapeJava(your_string));在Edittext中解码或取消unicode到笑脸
public static String unescapeJava(String escaped) {
if (escaped.indexOf("\\u") == -1)
return escaped;
String processed = "";
int position = escaped.indexOf("\\u");
while (position != -1) {
if (position != 0)
processed += escaped.substring(0, position);
String token = escaped.substring(position + 2, position + 6);
escaped = escaped.substring(position + 6);
processed += (char) Integer.parseInt(token, 16);
position = escaped.indexOf("\\u");
}
processed += escaped;
return processed;
}
答案 1 :(得分:0)