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

Mik*_*ker 11 unicode country android textview emoji

我已经看到,因为LollipopAndroid已经Emoji为不同的国家建立了标志.是否可以使用设备区域设置来检索该Emoji国家/地区的标志?

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

Dmi*_*try 32

表情符号是Unicode符号.基于Unicode字符表,表情符号标志由26个字母Unicode字符(AZ)组成,旨在用于编码ISO 3166-1 alpha-2双字母国家/地区代码(wiki).

这意味着可以拆分两个字母的国家/地区代码,并将每个AZ字母转换为区域指示符号:

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));
}
Run Code Online (Sandbox Code Playgroud)

其中0x41代表大写A字母和0x1F1E6REGIONAL INDICATOR SYMBOL LETTER A在Unicode编码表.

注意:此代码示例已简化,并且没有与国家/地区代码相关的必需检查,这些检查可能在区域设置中不可用.


I'm*_*gon 8

基于这个答案,我使用扩展功能在下面写了一个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))
}
Run Code Online (Sandbox Code Playgroud)

Runnable Code Snippet

我还包括一个使用Kotlin Playground的可运行的Kotlin片段.要运行代码段,您需要:

  1. 点击"显示代码段"
  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>
    Run Code Online (Sandbox Code Playgroud)


The*_*rga 0

我也在寻找这个,但我认为目前还不可能。

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

没有提到旗帜。

_

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

Android 国家/地区列表,其中包含标志以及获取 ISO 移动代码的可用性

这可能对你有帮助。