13

我已经搜索了很多,但我发现的东西有点混乱。

我需要获取 android 国家/地区列表并设置默认用户区域设置。

例如:我正在注册一个用户帐户,我需要插入国家,在那个微调器中将显示所有国家,但默认情况下会显示我的默认语言环境。

现在我有:

private Spinner spCountry;
private String array_spinner[];

...

spCountry = (Spinner) findViewById(R.id.spCountry);

array_spinner = new String[1];
array_spinner[0] = "Portugal";

ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, array_spinner);
spCountry.setAdapter(adapter);

谢谢大家的帮助!

4

4 回答 4

33

至于我,我迭代可用的语言环境并将每个项目添加到 arrayList 中。当然,我必须忽略重复项和空字符串。这是我的代码:

 SortedSet<String> countries = new TreeSet<>();
 for (Locale locale : Locale.getAvailableLocales()) {
     if (!TextUtils.isEmpty(locale.getDisplayCountry())) {
          countries.add(locale.getDisplayCountry());
         }
        }

Spinner citizenship = (Spinner)findViewById(R.id.input_citizenship);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, getCountryListByLocale().toArray(new String[0]));
citizenship.setAdapter(adapter);
于 2014-03-03T16:00:58.417 回答
8

您可以使用

private static final String DEFAULT_LOCAL = "Portugal";

然后使用它来设置默认选择,如下所示。

ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, array_spinner);
spCountry.setAdapter(adapter);
spCountry.setSelection(adapter.getPosition(DEFAULT_LOCAL));

输出:

在此处输入图像描述

更新arrays.xml: 创建res/values

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="country_arrays">
        <item>Malaysia</item>
        <item>United States</item>
        <item>Indonesia</item>
        <item>France</item>
        <item>Italy</item>
        <item>Singapore</item>
        <item>New Zealand</item>
        <item>India</item>
        <item>Portugal</item>
    </string-array>

</resources>

然后在你的中使用以下activity来获取所有国家。

array_spinner = getResources().getStringArray(R.array.country_arrays);
于 2013-09-03T13:55:10.367 回答
2

一个有用且可定制的国家选择器,可满足您的需求。

摇篮

repositories {
    maven { url "https://jitpack.io" }
}

compile 'com.github.ekimual:country-picker-x:1.0.0'

样品用法:

/* Declare */
CountryPickerDialog countryPicker;

/* Name of your Custom JSON list */
int resourceId = getResources().getIdentifier("country_avail", "raw", getApplicationContext().getPackageName());

countryPicker = new CountryPickerDialog(MainActivity.this, new  CountryPickerCallbacks() {
      @Override
      public void onCountrySelected(Country country, int flagResId) {
            /* Get Country Name: country.getCountryName(context); */
            /* Call countryPicker.dismiss(); to prevent memory leaks */
      }

      /* Set to false if you want to disable Dial Code in the results and true if you want to show it 
         Set to zero if you don't have a custom JSON list of countries in your raw file otherwise use 
         resourceId for your customly available countries */
  }, false, 0);

countryPicker.show();

参考https://android-arsenal.com/details/1/4390

于 2017-02-03T15:45:58.090 回答
0

为什么不这样做呢?只需将国家/地区放在列表中,对其进行排序并插入即可。

String[] locales = Locale.getISOCountries();
List<String> countries = new ArrayList<>();
countries.add("**Select Country**");

for (String countryCode : locales) {

        Locale obj = new Locale("", countryCode);

        countries.add(obj.getDisplayCountry());

}
Collections.sort(countries);
countrySpinner.setItems(countries);
于 2017-12-21T18:43:41.553 回答