Android 使用区域设置获取国家表情符号标志

Android Get Country Emoji Flag Using Locale

我看到自从 Lollipop 以来,Android 已经内置了 Emoji 不同国家的旗帜。是否可以使用设备区域设置检索该国家/地区的 Emoji 标志?

我想将 Emoji 标志插入到包含用户位置的 TextView 中。

我也在寻找那个,但我认为这不可能。

看看这里: http://developer.android.com/reference/java/util/Locale.html

没有提到标志。

_

或者您可以在此处查看答案:

Android Countries list with flags and availability of getting iso mobile codes

这可能对你有帮助。

Emoji 是一种 Unicode 符号。基于 Unicode 字符 table 表情符号标志由 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 table.

中是 REGIONAL INDICATOR SYMBOL LETTER A

注意:此代码示例已简化,没有与国家代码相关的必需检查,这在语言环境中可能不可用。

当我第一次写这个答案时,我以某种方式忽略了我只通过 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"));

如果您想将国家代码作为大写字母传递,只需将两个偏移量更改为 0x1f1a50xdda5

的基础上,我用扩展函数写了一个Kotlin版本如下

我还添加了一些检查来处理未知的国家代码。

/**
 * This method is to change the country code like "us" into 
 * Stolen from 
 * 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 的 运行nable Kotlin 片段。为了 运行 您需要的代码段:

  1. 点击"Show code snippet"
  2. 点击"Run Code Snippet"
  3. 点击生成控制台右上角的播放按钮
  4. 滚动到底部查看结果(隐藏..)
    <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 
     * 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&lt;String&gt;){
      println("us".toFlagEmoji())
      println("AF".toFlagEmoji())
      println("BR".toFlagEmoji())
      println("MY".toFlagEmoji())
      println("JP".toFlagEmoji())
    }
    
    </div>
    

我用起来很轻松。 从 here.

获取 Unicode

对于孟加拉国国旗,它是 U+1F1E7 U+1F1E9 现在,

{...

 String flag = getEmojiByUnicode(0x1F1E7)+getEmojiByUnicode(0x1F1E9)+ " Bangladesh";
    }
    public String getEmojiByUnicode(int unicode){
        return new String(Character.toChars(unicode));
    }

它将显示>(孟加拉国国旗)孟加拉国

您可以非常简单地获取国家代码。 我想谈谈根据国家代码选择国旗。

我写了一篇class,用起来很简单。

用法:

String countryWithFlag = CountryFlags.getCountryFlagByCountryCode("TR") + " " + "Türkiye";

输出:土耳其

您也可以将它与 Android TextView 一起使用:)

你可以看看classhere

它在 Android 6 及更高版本上运行良好。