【问题标题】:Android Get Country Emoji Flag Using LocaleAndroid 使用语言环境获取国家/地区 Emoji 标志
【发布时间】:2015-08-10 05:40:56
【问题描述】:

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

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

【问题讨论】:

    标签: android unicode textview emoji country


    【解决方案1】:

    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

    注意:此代码示例经过简化,不需要与国家/地区代码相关的检查,这在语言环境中可能不可用。

    【讨论】:

    • 我可以得到所有国家的标志吗?
    • java.util.Locale.getISOCountries() 方法返回 ISO 3166 中定义的所有 2 字母国家代码的列表。您可以使用这些字符串生成表情符号。
    • 这应该是公认的答案。这就是它应该如何完成的,也是在 iOS 上如何完成的。还有更多关于教育简化说明的荣誉!
    • Android 4.4 及更低版本的默认字体中没有这些表情符号。如果你想让它在这些上工作,你需要在应用程序中使用自己的字体(5.0 及更高版本的官方 Android 字体是 Noto Color Emoji :google.com/get/noto/#emoji-zsye-color
    • @Dmitry 我最终使用了我不知道的EmojiCompat API(来自支持库)——它解决了这个确切的问题,实际上只需两行代码即可集成
    【解决方案2】:

    基于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,您需要:

    1. 点击“显示代码 sn-p”
    2. 点击“运行代码片段”
    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 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&lt;String&gt;){
        println("us".toFlagEmoji())
        println("AF".toFlagEmoji())
        println("BR".toFlagEmoji())
        println("MY".toFlagEmoji())
        println("JP".toFlagEmoji())
      }
      
      </div>

    【讨论】:

    • 这适用于 kitkat 吗?当我调试它时,我看到问号?
    • 这在我的手机上完美运行,但它适用于较旧的 api 吗?
    【解决方案3】:

    当我第一次写这个答案时,我不知何故忽略了我只通过 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

    【讨论】:

      【解决方案4】:

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

      我写了一个关于它的类,使用起来非常简单。

      用法:

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

      输出:?? Türkiye

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

      您可以查看课程here

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

      【讨论】:

        【解决方案5】:

        我也在寻找它,但我认为这还不可能。

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

        没有提到标志。

        _

        您也可以在这里查看答案:

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

        这可能对你有帮助。

        【讨论】:

        • 感谢您的帮助。我能够使用设备国家代码找到表情符号。
        【解决方案6】:

        我很容易使用它。 从here 获取 Unicode。

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-07-10
          • 1970-01-01
          • 2014-06-17
          • 2016-06-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多