Java 如何保存用户';s语言环境设置(在应用程序中)?
我正在制作一个有两种可用语言的应用程序。用户通过单击按钮在不同语言之间切换。以下是我的onClick方法:Java 如何保存用户';s语言环境设置(在应用程序中)?,java,android,eclipse,sharedpreferences,Java,Android,Eclipse,Sharedpreferences,我正在制作一个有两种可用语言的应用程序。用户通过单击按钮在不同语言之间切换。以下是我的onClick方法: public void setLocaleEng(View v){ Locale localeEng = new Locale("en"); Locale.setDefault(localeEng); Configuration configEng = new Configuration(); configEng.locale = localeEng;
public void setLocaleEng(View v){
Locale localeEng = new Locale("en");
Locale.setDefault(localeEng);
Configuration configEng = new Configuration();
configEng.locale = localeEng;
getBaseContext().getResources().updateConfiguration(configEng, getBaseContext().getResources().getDisplayMetrics());
Intent intent = new Intent(NastavitveJezika.this, MainActivity.class);
finish();
startActivity(intent);
}
public void setLocaleSlo(View v){
Locale localeSlo = new Locale("sl");
Locale.setDefault(localeSlo);
Configuration configSlo = new Configuration();
configSlo.locale = localeSlo;
getBaseContext().getResources().updateConfiguration(configSlo, getBaseContext().getResources().getDisplayMetrics());
Intent intent = new Intent(NastavitveJezika.this, MainActivity.class);
finish();
startActivity(intent);
}
这可以正常工作,但当用户完全退出应用程序并重新打开它时,它将返回默认值(英语)。如何让我的应用程序记住用户选择的语言设置?如果答案是共享首选项,那么如何选择?到目前为止,我只使用共享首选项来存储字符串和布尔值,我不知道如何处理类似的事情
如果答案是共享首选项,那么如何选择?我只使用了共享的
存储字符串的首选项
是的,答案是SharedReferences,您可以使用它来存储字符串,就像以前一样。只需将其存储为“en”或“sl”,然后
String enOrSlReadFromSharedPrefs = readSharedPrefsJustLikeYouDidBefore();
Locale locale = new Locale(enOrSlReadFromSharedPrefs);
Locale.setDefault(locale);
Configuration configSlo = new Configuration();
configSlo.locale = localeSlo;
getBaseContext().getResources().updateConfiguration(configSlo, getBaseContext().getResources().getDisplayMetrics());
Intent intent = new Intent(NastavitveJezika.this, MainActivity.class);
finish();
startActivity(intent);