从Android操作系统中检索国家/地区列表 [英] Retrieve a list of countries from the android OS

查看:994
本文介绍了从Android操作系统中检索国家/地区列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来填充一个带有名称的国家列表的微调器。我可以从Android操作系统中检索它吗?有人可以给我一个例子吗?

I'm looking for a way to populate a spinner with a list of countries with their names. Can I retrieve it from the Android OS? Can someone please provide me an example?

推荐答案

你可以从 区域设置 类。

You might get some idea from the Locale class.

致电 getAvailableLocales() 然后迭代数组& getDisplayCountry() 。如果这是您第一次看到该国家/地区名称,请将其添加到可扩展列表中(例如 ArrayList 实例)。

在Java中,但来自的3个类java.util 在Android中都可用。

In Java, but the 3 classes from java.util are all available in Android.

import java.util.*;

class Countries {

    public static void main(String[] args) {
        Locale[] locales = Locale.getAvailableLocales();
        ArrayList<String> countries = new ArrayList<String>();
        for (Locale locale : locales) {
            String country = locale.getDisplayCountry();
            if (country.trim().length()>0 && !countries.contains(country)) {
                countries.add(country);
            }
        }
        Collections.sort(countries);
        for (String country : countries) {
            System.out.println(country);
        }
        System.out.println( "# countries found: " + countries.size());
    }
}



此台式电脑上的输出



Output on this desktop PC

Albania
Algeria
Argentina
Australia
..
Venezuela
Vietnam
Yemen
# countries found: 95
Press any key to continue . . .

这篇关于从Android操作系统中检索国家/地区列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆