如何将语言从英语更改为相同活动的印地语并保存为默认语言,直到它再次通过android中的微调器更改语言?
spinnerLanguage.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos == 1) {
Toast.makeText(parent.getContext(),
"You have selected Hindi", Toast.LENGTH_SHORT)
.show();
setLocale("hi");
spinnerLanguage.setSelection(pos);
} else if (pos == 2) {
Toast.makeText(parent.getContext(),
"You have selected English", Toast.LENGTH_SHORT)
.show();
setLocale("en");
spinnerLanguage.setSelection(pos);
}
}
答案 0 :(得分:1)
String languageToLoad = "en"; // your language
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
然后你应该像这样保存语言
SharedPreferences languagepref = getSharedPreferences("language",MODE_PRIVATE);
SharedPreferences.Editor editor = languagepref.edit();
editor.putString("languageToLoad",languageToLoad );
editor.commit();
答案 1 :(得分:0)
更改语言后,请将您的本地代码保存在sharedPreferences
和onStart()
设置的区域设置中。