【问题标题】:Country flag emoji not getting using country code in国旗表情符号未使用国家/地区代码
【发布时间】:2021-03-02 19:50:37
【问题描述】:

我想使用国家/地区代码获取国家/地区代码,API 19 to API 28 的所有版本都应支持该代码。为此,我搜索了很多并找到了答案,然后我选择了标记为已接受答案的代码。

我正在使用该答案中的代码

 private String localeToEmoji(Locale locale) {
    String countryCode = locale.getCountry();
    Log.v("Asdasdasdasd", countryCode+" ; "+locale.getDisplayCountry());
    int firstLetter = Character.codePointAt(countryCode.toUpperCase(), 0) - 0x41 + 0x1F1E6;
    int secondLetter = Character.codePointAt(countryCode.toUpperCase(), 1) - 0x41 + 0x1F1E6;
    return new String(Character.toChars(firstLetter)) + new String(Character.toChars(secondLetter));
}

我正在使用这个功能,

 Locale current = ConfigurationCompat.getLocales(getResources().getConfiguration()).get(0);
 txt.setText(localeToEmoji(current));

但这不适用于KitkatLollipop。它只显示国家代码而不是国旗。我现在已经在这两种设备上进行了测试。

那么使用国家代码检索国旗的最佳方法是什么。

我们将不胜感激高级帮助!

【问题讨论】:

    标签: android locale country-codes


    【解决方案1】:

    您的代码看起来不错。要做的事,需要使用Emoji Compatibility

    您应该使用以下代码从代码中获取标志,

     public String countryCodeToEmoji(String code) {
    
        // offset between uppercase ascii and regional indicator symbols
        int OFFSET = 127397;
    
        // validate code
        if (code == null || code.length() != 2) {
            return "";
        }
    
        //fix for uk -> gb
        if (code.equalsIgnoreCase("uk")) {
            code = "gb";
        }
    
        // convert code to uppercase
        code = code.toUpperCase();
    
        StringBuilder emojiStr = new StringBuilder();
    
        //loop all characters
        for (int i = 0; i < code.length(); i++) {
            emojiStr.appendCodePoint(code.charAt(i) + OFFSET);
        }
    
        // return emoji
        return emojiStr.toString();
    }
    

    您需要在 build.gradle 文件中添加 emoji 依赖项

    实现“com.android.support:support-emoji-bundled:28.0.0”

    如果你需要 FontRequest 那么你必须添加下面的依赖项

    实现“com.android.support:support-emoji:28.0.0”

    现在您必须创建应用程序类并使用配置初始化EmojiCompact

      EmojiCompat.Config config = new BundledEmojiCompatConfig(getApplicationContext());
      EmojiCompat.init(config);
    

    现在使用 androidx.emoji.widget.EmojiTextView 代替 TextView 并使用该方法,

    EmojiTextView txt = findViewById(R.id.txt);
    txt.setText(countryCodeToEmoji("IN")); // Here you can use country codes.
    

    这将从API 19 to API 28 latest 开始工作。

    现在已经完成了。

    【讨论】:

      【解决方案2】:
       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));
      
      }
      

      这里0x41代表大写字母,0x1F1E6是Unicode表中的REGIONAL INDICATOR SYMBOL LETTER A,使用又是toUpper,这可能会导致问题

      【讨论】:

        【解决方案3】:

        @Piyush 更新答案: (注意我的代码需要大写的国家代码)

        build.gradle:

            implementation 'androidx.emoji:emoji-bundled:1.1.0'
            implementation 'androidx.emoji:emoji:1.1.0'
        

        countryToEmojiCode:

        object CountryHelper {
            private const val flagsAsciiOffset = 127397
            fun countryToEmojiCode(countryCode: String) = StringBuilder().apply {
                val simCountryIso = if (countryCode == "UK") {
                    "GB"
                } else {
                    countryCode
                }
                simCountryIso.forEach {
                    appendCodePoint(it.toInt() + flagsAsciiOffset)
                }
            }.toString()
        }
        

        用法:

        val emojiInitializer = EmojiCompat.init(BundledEmojiCompatConfig(requireContext()))
        
        emojiInitializer.registerInitCallback(
                EmojiCompatHelper { initialised: Boolean ->
                    if (initialised) {
                        val flagEmoji = EmojiCompat.get().process(flagEmojiCode).toString()
                    }
                })
        

        表情符号库的初始化回调:

        class EmojiCompatHelper(private val initialised: (Boolean) -> Unit): EmojiCompat.InitCallback() {
            override fun onInitialized() {
                super.onInitialized()
                initialised(true)
            }
        
            override fun onFailed(throwable: Throwable?) {
                super.onFailed(throwable)
                initialised(false)
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-11-24
          • 1970-01-01
          • 1970-01-01
          • 2020-02-27
          相关资源
          最近更新 更多