如何在Android设备中选择当前语言?


当前回答

我的解是这样的

@SuppressWarnings("deprecation")
public String getCurrentLocale2() {
    return Resources.getSystem().getConfiguration().locale.getLanguage();
}

@TargetApi(Build.VERSION_CODES.N)
public Locale getCurrentLocale() {
    getResources();
    return Resources.getSystem().getConfiguration().getLocales().get(0);
}

然后

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Log.e("Locale", getCurrentLocale().getLanguage());
            } else {
                Log.e("Locale", getCurrentLocale2().toString());
            }

显示——>和

其他回答

基本的Kotlin答案:

Locale.getDefault().language

如果你想要获得你的设备所选择的语言,这可能会帮助你:

Locale.getDefault().getDisplayLanguage();

你可以使用Locale.getDefault().getLanguage();要获得常用的语言代码(例如:“德”、“en”)

您可以从当前区域“提取”语言。你可以通过标准的Java API或者使用Android Context来提取语言环境。例如,下面这两行是等价的:

String locale = context.getResources().getConfiguration().locale.getDisplayName();
String locale = java.util.Locale.getDefault().getDisplayName();

如果你使用喷气背包撰写

在可组合功能方面

Context.applicationContext.resources.configuration.locales.get(0).language 
if(Locale.getDefault().getDisplayName().equals("हिन्दी (भारत)")){
    // your code here
}