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

时间:2015-05-27 22:36:07

标签: android unicode textview emoji country

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

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

7 个答案:

答案 0 :(得分:25)

表情符号是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));
}

其中0x41表示大写A字母,0x1F1E6表示{Unicode}表中的REGIONAL INDICATOR SYMBOL LETTER A

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

答案 1 :(得分:8)

基于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代码段。为了运行该代码段,您需要:

  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>

答案 2 :(得分:1)

当我第一次写这个答案时,我忽略了我只通过React Native在Android上工作过!

无论如何,这是我的JavaScript解决方案,无论是否支持ES6,都可以使用。

&#13;
&#13;
0x1f1a5
&#13;
&#13;
&#13;

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

答案 3 :(得分:1)

如果要将表情符号国家/地区标志插入Android TextView(非常简单),请尝试以下方法: How to insert emoji flags into TextView Android

答案 4 :(得分:1)

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

我写了一个关于它的类,它很容易使用。

用法:

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

输出:??Türkiye

您也可以在Android TextView中使用它:)

您可以签出课程here

它在Android 6及更高版本上效果很好。

答案 5 :(得分:0)

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

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

没有提及旗帜。

_

或者你可以在这里查看答案:

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

可能对你有帮助。

答案 6 :(得分:0)

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

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

{...

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

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

相关问题