我有Android应用程序,我试图在应用程序中更改语言,它在Android +20 api ,,,但在api 18它的工作不好工作! 这是我的代码请帮帮我!
private void changeLanguage() {
if (getCurrentLanguage(context) == "en") {
Locale locale = new Locale("ar");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
onConfigurationChanged(config);
SharedPreferences settings = context.getSharedPreferences("Language", MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefEditor.putString("locale_override", "ar");
prefEditor.commit();
} else {
//
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config,
getResources().getDisplayMetrics());
onConfigurationChanged(config);
SharedPreferences settings = context.getSharedPreferences("Language", MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefEditor.putString("locale_override", "en");
prefEditor.commit();
}
Intent i = new Intent(context, SplashScreen.class);
startActivity(i);
}
答案 0 :(得分:0)
我猜您通过调用 changeLanguage()重新设置新语言的当前活动。如果是这样,首先我建议您在 changeLanguage()的末尾添加几行代码:
Intent i = new Intent(context, SplashScreen.class);
i.addFlags(FLAG_ACTIVITY_NEW_TASK); // start the activity in new task
startActivity(intent);
finish(); // finish the current activity
在SplashScreen onCreate()中添加:
SharedPreferences settings = getSharedPreferences("Language", MODE_PRIVATE);
String lang = settings.getString("locale_override","en"); // "en" is the default if there is no previously set language
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
这样,当您重新启动活动时,将根据语言设置设置区域设置。当您第一次开始活动时,语言将 en ,因为它被设置为lang字符串的默认值。如果您愿意,您当然可以将默认值更改为 ar 。
希望这有帮助。