您好我需要获取Android设备上定义的语言列表,并希望使用语言代码和语言名称填充Spinner。我怎样才能做到这一点?
在此先感谢和问候c。
答案
另一答案
使用:
Resources.getSystem().getAssets().getLocales();
另一答案
public static List getLanguages() {
String[] locales = Resources.getSystem().getAssets().getLocales();
List list = new ArrayList<>();
for (Locale locale : Locale.getAvailableLocales()) {
if (locale.getLanguage().length() == 2) {
if (!isLanguageInList(list, locale)) {
list.add(locale.getDisplayLanguage());
}
}
}
Collections.sort(list);
return list;
}
private static boolean isLanguageInList(List list, Locale locale) {
if (list == null) {
return false;
}
for (String item: list) {
if (item.equalsIgnoreCase(locale.getDisplayLanguage())){
return true;
}
}
return false;
}