如何在Android设备中选择当前语言?
当前回答
以上答案没有区分简体中文和繁体中文。 Locale.getDefault(). tostring()工作,返回“zh_CN”,“zh_TW”,“en_US”等。
参考:https://developer.android.com/reference/java/util/Locale.html, ISO 639-1是旧的。
其他回答
你可以使用 .getLanguage .setLanguage (Locale.forLanguageTag (Locale.getDefault () ())); 这很好
获取设备语言的正确方法如下:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return context.getResources().getConfiguration().getLocales().get(0);
} else {
return context.getResources().getConfiguration().locale;
}
希望能有所帮助。
再加上约翰·佩尔格里姆的回答
context.getResources().getConfiguration().locale
Locale.getDefault()
是等价的,因为android.text.format.DateFormat类可以互换使用这两者。
private static String zeroPad(int inValue, int inMinDigits) {
return String.format(Locale.getDefault(), "%0" + inMinDigits + "d", inValue);
}
and
public static boolean is24HourFormat(Context context) {
String value = Settings.System.getString(context.getContentResolver(),
Settings.System.TIME_12_24);
if (value == null) {
Locale locale = context.getResources().getConfiguration().locale;
// ... snip the rest ...
}
基本的Kotlin答案:
Locale.getDefault().language
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。
推荐文章
- 如何模拟Android杀死我的进程
- 禁用EditText闪烁光标
- Android Eclipse -无法找到*.apk
- 设置TextView文本从html格式的字符串资源在XML
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Android加载JS包失败
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 在Android应用程序中显示当前时间和日期
- BottomSheetDialogFragment的圆角