How to set emoji by unicode in a textview?

﹥>﹥吖頭↗ 提交于 2019-11-26 03:06:53

问题


Hi I\'d like to do the following:

??? unicode = U+1F60A
String emoji = getEmojiByUnicode(unicode)
String text = \"So happy \"
textview.setText(text + emoji);

to get this in my textview:

So happy 😊

How can I implement getEmojiByUnicode(unicode)?

What type should the unicode variable be? (String, char, int?)

Please note that I do NOT want to use Drawables!


回答1:


Found a solution:

In my unicode I replaced 'U+' by '0x'

Example: replace 'U+1F60A' by '0x1F60A'

This way I got an 'int' like

int unicode = 0x1F60A;

Which can be used with

public String getEmojiByUnicode(int unicode){
    return new String(Character.toChars(unicode));
}

So Textview displays 😊 without Drawable

Try it with http://apps.timwhitlock.info/emoji/tables/unicode




回答2:


You can directly use Emojis in string resources by using the decimal code like this:

😊

for example:

<string name="emoji">I am happy &#128522;</>



回答3:


Note: For Kotlin

fun getEmoji(unicode: Int): String {
    return String(Character.toChars(unicode))
}


来源:https://stackoverflow.com/questions/26893796/how-to-set-emoji-by-unicode-in-a-textview

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