【问题标题】:Get country name String from country code int从国家代码 int 中获取国家名称字符串
【发布时间】:2017-12-02 21:55:37
【问题描述】:

我是 Java Android 新手。

我想使用Locale(参考ISO 3166-1 numeric)中的国家/地区代码获取国家/地区名称(字符串)

试图做类似的事情(其中 [...].getCountry() 返回 int 826):

Locale countryName = new Locale("", String.valueOf(profile.getPersonnalInformation().getAddress().getCountry())); 

并使用以下命令获取国家/地区名称:countryName.getDisplayCountry()

它通常应该返回“United Kingdom”,但不幸的是我有 826。

我该怎么做?

提前感谢您的帮助,

【问题讨论】:

  • @MehdiKhademloo 谢谢你,但我尝试用 int 数字而不是你的例子中的字符串来做。我已经尝试将我的 int 转换为 String 并使用它,但它不起作用,所以你的链接对我没有帮助。在您发布之前我已经阅读了它。
  • 这很重要,因为当您读取手机中的联系人时,您会得到一个数字(2 位),但当您使用 libphonenumber 时,您会输入一个 alpha-2..

标签: android locale country-codes


【解决方案1】:

现在,国家代码 (ISO 3166-1alpha-2/alpha-3/numeric) 列表作为 Java 枚举在 GitHub 上的 Apache 许可证版本 2.0 下可用。

示例:

int c_code=profile.getPersonnalInformation().getAddress().getCountry());// 392

CountryCode cc = CountryCode.getByCode(c_code);

String country_name=cc.getName();              // "Japan"

Gradle

dependencies {

  compile 'com.neovisionaries:nv-i18n:1.20'

}

Github

https://github.com/TakahikoKawasaki/nv-i18n

【讨论】:

    【解决方案2】:

    您必须获得国家代码字符串(2 或 3 个字母)才能使用它。 github上有一个开源库可以为您完成。见Is there an open source java enum of ISO 3166-1 country codes 或者只取https://subversivebytes.wordpress.com/2013/10/07/java-iso-3166-java-enum/的枚举

    【讨论】:

      猜你喜欢
      • 2016-06-11
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多