How can I put a 💙, or any other emoji inside an XML string?

别说谁变了你拦得住时间么 提交于 2019-12-19 05:55:16

问题


How can I do this? I'm pretty new to Java and Android and I have the problem described above. When I paste the emoji inside the xml file it shows a white square and another weird character which "copies" the next character.

Any idea on how to work this out?


回答1:


Try using this library - emoji-java
I know you want an XML way, and this is Java
It may help you

Example

String str = "An 😀awesome 😃string with a few 😉emojis!";
String result = EmojiParser.parseToAliases(myString);
System.out.println(myString);
// Prints:
// "An 😀awesome 😃string with a few 😉emojis!"



回答2:


You can put emojis in an XML, and the squares are just the editor's way to render characters it can't recognize, like all other characters, you can specify the character by its UTF8 value instead.

For example, 😃 has the char value of 128515, so you could use &#128515; to specify it. It is very similar to how you need to use &lt; to specify a literal '<' in an xml.

HOWEVER, as of writing this answer, using these tokens in (for example) strings.xml as string constants will cause your app to crash, with an error going all the way down to the JNI level of your android application (C++ code).

JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0xf0

I am not qualified to say how much this can limit your use of emojis in android in general, but I can say for certain that you can't add emojis to android xml files (without making your app crash, that is).



来源:https://stackoverflow.com/questions/33288225/how-can-i-put-a-or-any-other-emoji-inside-an-xml-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!