我看到自从 Lollipop
以来,Android 已经为不同的国家内置了 Emoji
标志。是否可以使用设备区域设置检索该国家/地区的 Emoji
标志?
我想将 Emoji
标志插入到包含用户位置的 TextView
中。
最佳答案
Emoji 是一种 Unicode 符号。基于 Unicode 字符表,表情符号标志由 26 个字母 Unicode 字符 (A-Z) 组成,旨在用于编码 ISO 3166-1 alpha-2 双字母国家代码 ( wiki )。
这意味着可以拆分两个字母的国家代码并将每个 A-Z 字母转换为区域指示符号字母:
private String localeToEmoji(Locale locale) {
String countryCode = locale.getCountry();
int firstLetter = Character.codePointAt(countryCode, 0) - 0x41 + 0x1F1E6;
int secondLetter = Character.codePointAt(countryCode, 1) - 0x41 + 0x1F1E6;
return new String(Character.toChars(firstLetter)) + new String(Character.toChars(secondLetter));
}
或者在 Kotlin 中,例如(假设为 UTF-8):
val Locale.flagEmoji: String
get() {
val firstLetter = Character.codePointAt(country, 0) - 0x41 + 0x1F1E6
val secondLetter = Character.codePointAt(country, 1) - 0x41 + 0x1F1E6
return String(Character.toChars(firstLetter)) + String(Character.toChars(secondLetter))
}
其中 0x41
代表大写的 A
字母,0x1F1E6
是 Unicode 表中的 REGIONAL INDICATOR SYMBOL LETTER A
。
注意:此代码示例经过简化,没有与国家代码相关的必需检查,这在语言环境中可能不可用。
关于Android 使用区域设置获取国家表情符号标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30494284/