Android 无法识别某些 unicode 值(表情符号)

Android some unicode values (emoticons) not recognized

我正在使用上面的代码在 EditText:

中显示一些包含表情符号的文本
EditText et = (EditText) findViewById(R.id.myeditext);
et.setText(StringEscapeUtils.unescapeJava("This is a text with emoji \u263A"));

这显示了我写的文字和笑脸表情或其他东西。

但是如果我输入另一个值而不是 \u263A,例如 \u1F60A,它就不起作用。它在这里显示了这个问题中的图像:

Unicode character (U+1FXYZ) not outputting correctly when used in code-behind

有人知道怎么处理吗? 谢谢。

更新

当包含 unicode 的字符串是随机的时,我如何使用下面给出的答案,甚至是假设的重复问题中给出的答案?

这是我想要实现的伪代码:

for ( eachFbComment as (String) randomString ) {
    //randomString example: "This is a text with emoji \u263A, string countinues here with another emoji \u1F60A, and a last emoji here \u263A! "
    print (randomString); // Here I want to display the text + emojis instead of unicode characters.
}

\uXXXX代表4个十六进制数字,16位Unicode。一些(不是 java)语言使用大写字母 \UXXXXXXXX (\U0001F60A)。您可以使用:

String emoji = new String(new int[] { 0x1F60A }, 0, 1);

这使用只有一个代码点的代码点数组。

et.setText("This is a text with emoji " + emoji);

是否显示表情符号取决于字体。


有问题的更新后:

大小写:字符串包含一个反斜杠,'u'和4到5个十六进制数字。

String s = "This is with \u263A, continuing with another \u1F60A, and \u263A!";

请注意,在 java 中,"\u1F60A" 将是两个代码点,用于 '\u1F60''A'。所以上面是自己制定的约定,就类似于java的Unicode u-escaping。一目了然 \u1F60A.

s翻译成完整的Unicode字符串:

Pattern pattern = Pattern.compile("\\u([0-9A-Fa-f]{4,5})\b");
StringBuffer sb = new StringBuffer();
Matcher m = pattern.matcher(s);
while (m.find()) {
    int cp = Integer.parseInt(m.group(1), 16);
    String added = cp < 0x10000
        ? String.valueOf((char) cp)
        : new String(new int[] { cp }, 0, 1);
    m.appendReplacement(sb, added);
}
m.appendTail(sb);
s = sb.toString();