我使用此代码段允许用户在应用程序中设置他最喜欢的语言环境:
Locale locale = new Locale(newLan);
Locale.setDefault(locale);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
问题是我想编写一个设置,允许用户返回到手机的默认语言。
这怎么可能?
因为在使用上面的代码 fragment 并假设用户选择法语后,我无法取回电话区域设置(例如可能是英语)
最佳答案
我刚刚试过了,我的手机语言环境是美国,toast 以法语显示,但在日志中我仍然看到美国,也许如果你不将新语言环境设置为默认值它仍然可以工作吗?
Locale locale = new Locale("fr");
//Locale.setDefault(locale);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
Toast.makeText(this, android.R.string.cancel, Toast.LENGTH_LONG).show();
Log.d("LOCALE", Locale.getDefault().getCountry());
我在其他问题和答案中看到过使用 Locale.setDefault()
,现在我想知道,为什么您需要手动设置默认的 Locale
?如果那是必要的,它不会在 updateConfiguration()
中完成吗? this answer也很有趣
关于android - 在我设置了 Locale.setDefault(locale) 之后,我怎样才能取回电话语言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42486982/