android 国旗表情符号无法使用国家/地区代码

jm2pwxwz  于 2022-11-27  发布在  Android
关注(0)|答案(4)|浏览(277)

我想得到国旗使用国家代码,这应该是支持所有版本从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中不起作用。它只显示国家代码而不是国旗。我现在已经在这两个设备中测试过了。
那么什么是最好的方法来检索国旗使用国家代码。
如能提前提供帮助,将不胜感激!

polkgigr

polkgigr1#

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表示大写字母A,0x1F1E6是Unicode表中的REGIONAL INDICATOR SYMBOL LETTER A,并且用户再次使用toUpper,这可能会导致问题

kgsdhlau

kgsdhlau2#

@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)
    }
}
8fsztsew

8fsztsew3#

使用Kotlin函数将country code转换为Flag表情符号。

fun countryFlag(code: String) = code
  .uppercase()
  .split("")
  .filter { it.isNotBlank() }
  .map { it.codePointAt(0) + 0x1F1A5 }
  .joinToString("") { String(Character.toChars(it)) }

println(countryFlag("in"))
// output 🇮🇳
fykwrbwg

fykwrbwg4#

添加扩展名:

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.uppercase() // 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))
    }

这样命名它:

holder.countryFlag.text = countryCode.iso2.toFlagEmoji()

相关问题