如何在Android设备中选择当前语言?
当前回答
Locale.getDefault().getDisplayLanguage()
会给你语言的书面名称,例如,英语,荷兰语,法语
Locale.getDefault().getLanguage()
会给你语言代码,例如:en, nl, fr
两种方法都返回String
其他回答
你可以尝试从系统资源中获取locale:
PackageManager packageManager = context.getPackageManager();
Resources resources = packageManager.getResourcesForApplication("android");
String language = resources.getConfiguration().locale.getLanguage();
Locale.getDefault().getLanguage()是虚拟机语言
Locale.getDefault().getLanguage()
它是当前运行应用程序的虚拟机实例的语言。它由java类(如DateFormat等)使用。如果您使用某些java类,则在更改应用程序区域设置时可能需要修改此选项。如果你在改变你的App语言环境时修改了这个,它与android的语言是不一样的。
context.getConfiguration().locale.getLanguage()是活动语言
context.getConfiguration().locale.getLanguage()
这是您活动中设置的语言。 在最新的SDK版本中,以下是可取的
context.getConfiguration().getLocales().get(0).getLanguage()
Resources.getSystem().getConfiguration().getLocales()给出用户在系统级添加的所有地区
这将为您提供用户在系统级别设置的第一个区域设置。
Resources.getSystem().getConfiguration().getLocales().get(0).getLanguage()
大量的用户是多语言的,所以你可能想要循环使用locale。
if(Locale.getDefault().getDisplayName().equals("हिन्दी (भारत)")){
// your code here
}
如果你使用喷气背包撰写
在可组合功能方面
Context.applicationContext.resources.configuration.locales.get(0).language
基本的Kotlin答案:
Locale.getDefault().language
推荐文章
- 我如何在一个片段中访问getSupportFragmentManager() ?
- 调试在哪里。Android Studio中的密钥存储库
- 从资产中读取文件
- 在不活动的地方调用getLayoutInflater()
- 设置Android布局元素的背景颜色
- 错误:'keytool'不能被识别为内部或外部命令、可操作程序或批处理文件
- 在应用程序本身中更改Locale
- apk (.apk)和应用程序包(.aab)的区别
- 如何设置超时在改造库?
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- TextView的字体大小在Android应用程序改变字体大小从本机设置
- 如何模拟Android杀死我的进程
- 禁用EditText闪烁光标
- Android Eclipse -无法找到*.apk
- 设置TextView文本从html格式的字符串资源在XML