android - 在我设置了 Locale.setDefault(locale) 之后,我怎样才能取回电话语言?

标签 android locale

我使用此代码段允许用户在应用程序中设置他最喜欢的语言环境:

 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/

相关文章:

android - Recyclerview行项目选择颜色变化

android - 无法从 api 获取数据,Future 总是返回 null - flutter

android - 在应用计费中实现哪种方式?

android - 为Android应用程序设计UI时如何确定图像的大小

java - 仅在本地计算机 JVM 上设置区域设置

c - C 库最初如何读取语言环境文件

c++ - 如何在 C++ 中读取 Cyrillic Unicode 文件?

regex - 语言环境何时会影响 R 的正则表达式?

android - 可处理实时通话的移动应用程序

python - 使用 Python 2.X 的语言环境模块格式化数字和货币