how set emoji by unicode in a textview?

2019-01-03 23:05发布

Hi I'd like to do the following:

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

to get this in my textview:

So happy

3条回答
Melony?
2楼-- · 2019-01-03 23:24

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

查看更多
▲ chillily
3楼-- · 2019-01-03 23:32

NOte: For Kotlin

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

查看更多
太酷不给撩
4楼-- · 2019-01-03 23:45

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

�

for example:

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

NOTE: The x is required in android vs standard XML escape sequences.

查看更多
登录 后发表回答