【发布时间】:2015-08-10 05:40:56
【问题描述】:
我看到自从Lollipop 以来,Android 已经为不同国家/地区内置了Emoji 标志。是否可以使用设备语言环境来检索该国家/地区的 Emoji 标志?
我想将Emoji 标志插入到包含用户位置的TextView。
【问题讨论】:
标签: android unicode textview emoji country
我看到自从Lollipop 以来,Android 已经为不同国家/地区内置了Emoji 标志。是否可以使用设备语言环境来检索该国家/地区的 Emoji 标志?
我想将Emoji 标志插入到包含用户位置的TextView。
【问题讨论】:
标签: android unicode textview emoji country
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。
注意:此代码示例经过简化,不需要与国家/地区代码相关的检查,这在语言环境中可能不可用。
【讨论】:
EmojiCompat API(来自支持库)——它解决了这个确切的问题,实际上只需两行代码即可集成
基于this answer,我在下面使用扩展函数写了一个Kotlin版本。
我还添加了一些检查来处理未知的国家代码。
/**
* This method is to change the country code like "us" into ??
* Stolen from https://stackoverflow.com/a/35849652/75579
* 1. It first checks if the string consists of only 2 characters: ISO 3166-1 alpha-2 two-letter country codes (https://en.wikipedia.org/wiki/Regional_Indicator_Symbol).
* 2. It then checks if both characters are alphabet
* do nothing if it doesn't fulfil the 2 checks
* caveat: if you enter an invalid 2 letter country code, say "XX", it will pass the 2 checks, and it will return unknown result
*/
fun String.toFlagEmoji(): String {
// 1. It first checks if the string consists of only 2 characters: ISO 3166-1 alpha-2 two-letter country codes (https://en.wikipedia.org/wiki/Regional_Indicator_Symbol).
if (this.length != 2) {
return this
}
val countryCodeCaps = this.toUpperCase() // upper case is important because we are calculating offset
val firstLetter = Character.codePointAt(countryCodeCaps, 0) - 0x41 + 0x1F1E6
val secondLetter = Character.codePointAt(countryCodeCaps, 1) - 0x41 + 0x1F1E6
// 2. It then checks if both characters are alphabet
if (!countryCodeCaps[0].isLetter() || !countryCodeCaps[1].isLetter()) {
return this
}
return String(Character.toChars(firstLetter)) + String(Character.toChars(secondLetter))
}
可运行代码片段
我还使用 Kotlin Playground 包含了一个可运行的 Kotlin sn-p。为了运行 sn-p,您需要:
<script src="https://unpkg.com/kotlin-playground@1.6.0/dist/playground.min.js" data-selector=".code"></script>
<div class="code" style="display:none;">
/**
* This method is to change the country code like "us" into ??
* Stolen from https://stackoverflow.com/a/35849652/75579
* 1. It first checks if the string consists of only 2 characters: ISO 3166-1 alpha-2 two-letter country codes (https://en.wikipedia.org/wiki/Regional_Indicator_Symbol).
* 2. It then checks if both characters are alphabet
* do nothing if it doesn't fulfil the 2 checks
* caveat: if you enter an invalid 2 letter country code, say "XX", it will pass the 2 checks, and it will return unknown result
*/
fun String.toFlagEmoji(): String {
// 1. It first checks if the string consists of only 2 characters: ISO 3166-1 alpha-2 two-letter country codes (https://en.wikipedia.org/wiki/Regional_Indicator_Symbol).
if (this.length != 2) {
return this
}
val countryCodeCaps = this.toUpperCase() // upper case is important because we are calculating offset
val firstLetter = Character.codePointAt(countryCodeCaps, 0) - 0x41 + 0x1F1E6
val secondLetter = Character.codePointAt(countryCodeCaps, 1) - 0x41 + 0x1F1E6
// 2. It then checks if both characters are alphabet
if (!countryCodeCaps[0].isLetter() || !countryCodeCaps[1].isLetter()) {
return this
}
return String(Character.toChars(firstLetter)) + String(Character.toChars(secondLetter))
}
fun main(args: Array<String>){
println("us".toFlagEmoji())
println("AF".toFlagEmoji())
println("BR".toFlagEmoji())
println("MY".toFlagEmoji())
println("JP".toFlagEmoji())
}
</div>
【讨论】:
当我第一次写这个答案时,我不知何故忽略了我只通过 React Native 在 Android 上工作过!
无论如何,这是我的 JavaScript 解决方案,无论是否支持 ES6。
function countryCodeToFlagEmoji(country) {
return typeof String.fromCodePoint === "function"
? String.fromCodePoint(...[...country].map(c => c.charCodeAt() + 0x1f185))
: [...country]
.map(c => "\ud83c" + String.fromCharCode(0xdd85 + c.charCodeAt()))
.join("");
}
console.log(countryCodeToFlagEmoji("au"));
console.log(countryCodeToFlagEmoji("aubdusca"));
如果您想将国家代码作为大写字母传递,只需将两个偏移量更改为0x1f1a5 和0xdda5。
【讨论】:
您可以非常简单地获得国家代码。 我想谈谈根据国家代码选择国旗。
我写了一个关于它的类,使用起来非常简单。
用法:
String countryWithFlag = CountryFlags.getCountryFlagByCountryCode("TR") + " " + "Türkiye";
输出:?? Türkiye
您也可以将它与 Android TextView 一起使用 :)
您可以查看课程here
它在 Android 6 及更高版本上运行良好。
【讨论】:
我也在寻找它,但我认为这还不可能。
看看这里: http://developer.android.com/reference/java/util/Locale.html
没有提到标志。
_
您也可以在这里查看答案:
Android Countries list with flags and availability of getting iso mobile codes
这可能对你有帮助。
【讨论】:
我很容易使用它。 从here 获取 Unicode。
孟加拉国国旗是U+1F1E7 U+1F1E9
现在,
{...
String flag = getEmojiByUnicode(0x1F1E7)+getEmojiByUnicode(0x1F1E9)+ " Bangladesh";
}
public String getEmojiByUnicode(int unicode){
return new String(Character.toChars(unicode));
}
它将显示 >(孟加拉国国旗)孟加拉国
【讨论】: