Java 在 Android TextView 中显示表情符号/情感图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9740565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display emoji/emotion icon in Android TextView
提问by mobile app Beginner
I have some problem with displaying emoji icon in Android TextView
我在 Android TextView 中显示表情符号图标时遇到了一些问题
First, I found a list of emoji icon in unicode at here: http://www.easyapns.com/category/just-for-fun
首先,我在这里找到了 unicode 中的表情符号图标列表:http: //www.easyapns.com/category/just-for-fun
Second, I found how to display emoji icon at here: https://github.com/sharakova/EmojiTextView/blob/master/src/jp/sharakova/android/emoji/EmojiTextView.java
其次,我在这里找到了如何显示表情符号图标:https: //github.com/sharakova/EmojiTextView/blob/master/src/jp/sharakova/android/emoji/EmojiTextView.java
The EmojiTextView.java can convert the predefined character to emoji icon automatically. Therefore, I want to replace all the occurrences of emoji icon in a String to some predefined character, and put the result to EmojiTextView.java The problem is my code cannot recognize emoji icon in the String which contains emoji icon.
EmojiTextView.java 可以自动将预定义的字符转换为表情符号图标。因此,我想将字符串中所有出现的表情符号图标替换为某个预定义的字符,并将结果放入 EmojiTextView.java 问题是我的代码无法识别包含表情符号图标的字符串中的表情符号图标。
Here is my code snippet - I am trying to find if the input match any unicode of emoji icon:
这是我的代码片段 - 我正在尝试查找输入是否与表情符号图标的任何 unicode 匹配:
// Array list of all emoji icon
private static final String[] ArrayEUnicodeString ={
"\uE415",
"\uE056",
"\uE057",
...
}
// Nothing matched when it receive emoji icon with unicode "\uE415" from iphone. 'input' is message received from XMPP server
for (int i=0; i < emojiLength; i++)
{
if (input.getBytes() == ArrayEUnicodeString[i].getBytes())
Log.e("test", "ArrayEUnicodeString found");
}
// Note: iphone can display the emoji icon if I send "\uE415"
I am not good at unicode comparison/convention. Can somebody help me please, thanks!
我不擅长 unicode 比较/约定。有人可以帮我吗,谢谢!
采纳答案by Jamal
You could also try to find the emoji using a regular expression: "[\ue415\ue056\ue057]", instead of comparing the bytes.
您还可以尝试使用正则表达式“[\ue415\ue056\ue057]”来查找表情符号,而不是比较字节。
回答by greg7gkb
It works fine if you convert the string to a char array and check each char, such as:
如果将字符串转换为字符数组并检查每个字符,则它工作正常,例如:
StringBuilder sb = new StringBuilder();
for (char curr : str.toCharArray()) {
sb.append((SUPPORTED_EMOJI_SET.contains(curr)) ? convertCharToImgTag(curr) : curr);
}
where SUPPORTED_EMOJI_SET is just a set of chars, for example:
其中 SUPPORTED_EMOJI_SET 只是一组字符,例如:
new HashSet<Character>() {{
add('\ue415');
add('\ue056');
...
}}
You could also do this with a regex but I believe the above would run faster.
你也可以用正则表达式来做到这一点,但我相信上面的会运行得更快。
回答by caw
Why do you want to embed the protected Apple emoji images in your application at all?
为什么要在应用程序中嵌入受保护的 Apple 表情符号图像?
The Unicode standard includes 722 emoji that can be displayed by Android's default font just by entering the Unicode chars into an EditText
field or TextView
.
Unicode 标准包括 722 个表情符号,只需将 Unicode 字符输入EditText
字段或TextView
.
You can, in addition, use the following library (in folder "Java") to automatically convert popular emoticons like :-)
to the corresponding Unicode emoji:
此外,您可以使用以下库(在文件夹“Java”中)自动将流行的表情符号转换:-)
为相应的 Unicode 表情符号:
回答by Girish Bhalerao
Here, Please go through below solution :
在这里,请通过以下解决方案:
Problem :Inside TextView instead of Emoji, String \ue415\ue056\ue057is showing.
问题:在 TextView 而不是 Emoji 中,显示的是 String \ue415\ue056\ue057。
Root cause :In java or android, programmatically string representation of Emoji's you will get as \\ue415\\ue056\\ue057. But when you try to print same String in console or LogCat then escape character is removed and you will get string as \ue415\ue056\ue057because of which root cause of this issue is not detectable.
根本原因:在 java 或 android 中,以编程方式将 Emoji 的字符串表示形式为\\ue415\\ue056\\ue057。但是,当您尝试在控制台或 LogCat 中打印相同的字符串时,转义字符将被删除,您将获得字符串\ue415\ue056\ue057,因为无法检测到此问题的根本原因。
Solution :To solve this issue, we need to handle escape character. I have created below method which solve this problem.
解决方案:为了解决这个问题,我们需要处理转义字符。我创建了以下方法来解决这个问题。
public static String getEmojiFromString(String emojiString) {
if (!emojiString.contains("\u")) {
return emojiString;
}
String emojiEncodedString = "";
int position = emojiString.indexOf("\u");
while (position != -1) {
if (position != 0) {
emojiEncodedString += emojiString.substring(0, position);
}
String token = emojiString.substring(position + 2, position + 6);
emojiString = emojiString.substring(position + 6);
emojiEncodedString += (char) Integer.parseInt(token, 16);
position = emojiString.indexOf("\u");
}
emojiEncodedString += emojiString;
return emojiEncodedString;
}