gpt4 book ai didi

java - 从 android 操作系统中检索国家/地区列表

转载 作者:IT老高 更新时间:2023-10-28 21:04:11 27 4
gpt4 key购买 nike

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

最佳答案

您可能会从 Locale 获得一些想法类(class)。

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


例如

在 Java 中,但 java.util 中的 3 个类在 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());
}
}

在这台台式电脑上输出

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

关于java - 从 android 操作系统中检索国家/地区列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9760341/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com