【发布时间】:2018-02-15 09:17:37
【问题描述】:
我正在我的 Android 应用程序中添加多种语言功能。
每当我更改语言时,它都会改变。但重新打开应用程序后,它再次显示选择语言屏幕。
我想保存最后选择的语言,所以下次当用户重新打开应用程序时,它不应该显示选择语言屏幕,它应该直接转到下一页,并且应该以最后选择的语言显示项目。
该怎么办?有什么解决办法吗?
请检查以下代码。
在这段代码中
我必须存储在 SharedPreferences 中的位置以及我必须获取 sharedpreference 的位置
@覆盖 public void onItemSelected(SelectableItem selectableItem) {
List<Item> selectedItems = adapter.getSelectedItems();
if(selectableItem.getName().equals("English")){
if (userSessionManager.isLoggedIn()) {
Intent intent = new Intent(LanguageListActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
setLanguage("en");
} else {
Intent intent = new Intent(LanguageListActivity.this, LoginActivity.class);
Log.d("Login", "firgage");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
setLanguage("en");
}
}else if(selectableItem.getName().equals("Hindi(हिंदी)")){
if (userSessionManager.isLoggedIn()) {
Intent intent = new Intent(LanguageListActivity.this, MainHindiActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
setLanguage("hi");
} else {
Intent intent = new Intent(LanguageListActivity.this, LoginhindiActivity .class);
Log.d("hLogin", "firhin");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
setLanguage("hi");
String lang = "hi";
}
}
}
protected void setLanguage(String language){
mylocale=new Locale(language);
Resources resources=getResources();
DisplayMetrics dm=resources.getDisplayMetrics();
Configuration conf= resources.getConfiguration();
conf.locale=mylocale;
resources.updateConfiguration(conf,dm);
//Intent refreshIntent=new Intent(LanguageListActivity.this,MainActivity.class);
finish();
//startActivity(refreshIntent);
}
【问题讨论】:
-
将其保存在某种配置文件中。如果您的应用程序具有登录功能,那么您可以保存为用户偏好。
-
分享你的代码
-
优先保存,然后在您返回应用程序后获取
-
将所选语言保存在共享偏好中。
-
在您添加
setLanguage("hi");时的代码中,通过将 hi 存储到共享首选项来添加我的 store in sharedpreference 答案
标签: android string android-studio android-activity multilingual