如何在文本视图中设置 Unicode 表情符号?

嗨,我想做以下事情:

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

把这个放进我的短信视频里:

太开心了

如何实现 getEmojiByUnicode(unicode)

unicode变量应该是什么类型? (String,char,int?)

请注意,我不想使用绘制!

95887 次浏览

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

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

😊

for example:

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

Note: For Kotlin

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

I think I found the most straightforward solution. In my case, I wanted to add a fire (🔥) emoji to one of the chips in a chip group. I simply went to the Emojipedia Fire Entry1, clicked on the copy button just below the emoji meaning, and literally just pasted it into my Kotlin code. Here is a code snippet of how it looked like after pasting.

val chip = Chip(context)
chip.text = "\uD83D\uDD25 New"

Here is how the code looks like once I run it on my device. I included the other chips as well 😉;

An image sample of how the chip looks like in the device

PS: I ran this on the latest version of Android Studio (Arctic Fox v. 2020.3.1). Results may differ with older versions.

Footnote

  1. Emojipedia is a free encyclopedia that lists and provides meanings for all the emojis approved under the Unicode standard. You can always head out there for insightful emoji meanings and for other emoji needs.

all of the credit to Kenneth Murerwa, whose answer solved my problem. just chiming in that just copy and paste what you get from the 'copy' button at https://emojipedia.org between good old quotation marks. Yeah, it's a noob point but hey, we're all noobs at the beggining 😂

val emoji = "\uD83D\uDE00 \uD83D\uDC4C"

and then you can add it to whatever string you need. It renders on the phone screen fine, though it won't show up in a println

println("👌")

You can do as below:

Unicode : uni-1F4A1

FYI, I am using Kotlin.

Create utility function as below:

private fun getEmojiByUnicode(reactionCode: String): String {
val code = reactionCode.substring(4).toInt(16)
return String(Character.toChars(code))
}

Where substring(4) will be discarded uni- these 3 characters and you have 1F4A1.

Set Emoji into TextView: (I am using ViewBinding in my Project)

mViewBinding.textViewEmoji.text = getEmojiByUnicode(data.Reaction)

For more details: Integer.parseInt ("0x1F60A") ends with NumberformatException

// example of unicode emoji - "U+1F4C1"
// other formats will return empty string
fun unicodeEmojiToHtmlEmoji(emoji: String): CharSequence {
val inEmojiPrefix = "U+"
val outEmojiPrefix = "&#x"
val outEmojiSuffix = ";"
return try {
HtmlCompat.fromHtml(
emoji.replace(
inEmojiPrefix,
outEmojiPrefix, true) + outEmojiSuffix,
HtmlCompat.FROM_HTML_MODE_LEGACY
)
} catch (e: Throwable) {
""
}
}


// example of html emoji - "&#x1F4C1;"
// other formats will return empty string
fun htmlEmojiToUnicodeEmoji(emoji: String): CharSequence {
val outEmojiPrefix = "U+"
return if(emoji.isNotBlank()) outEmojiPrefix + emoji.codePointAt(0).let(Integer::toHexString) else ""
}