我在Google上搜索& stackoverflow,但我無法解決我的問題。我指的是http://www.androhub.com/android-building-multi-language-supported-app/我有三項活動(活動1,2,3)。活動1>活動2>活動3(米爾蒂朗變化)。在練習3中,我有兩個單選按鈕來改變(en,hi)。我改變語言之後;去第一次活動(活動1),沒關係。但我想保留當前活動(刷新當前活動),選擇單選按鈕後(更改語言)。否則,當我回到onBackPressed()時,如何調用onCreate()。如何在改變語言後保持當前活動的最佳方式。如何更改語言後更新活動?
的onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
txt = (MMTextView) findViewById(R.id.txt);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
pref = getSharedPreferences("MyPref", Activity.MODE_PRIVATE);
editor = pref.edit();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
String lang = "en";
switch (i) {
case R.id.eng:
lang = "en";
Intent i1 = new Intent(Main3Activity.this, MainActivity.class);
i1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i1);
finish();
break;
case R.id.hi:
lang = "hi";
Intent i2 = new Intent(Main3Activity.this, MainActivity.class);
i2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i2);
finish();
break;
default:
break;
}
changeLocale(lang);
}
});
loadLocale();
}
changeLocale
private void changeLocale(String lang) {
if (lang.equalsIgnoreCase(""))
return;
Locale myLocale = new Locale(lang);
saveLocale(lang);
Locale.setDefault(myLocale);
Configuration config = new Configuration();
config.locale = myLocale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
saveLocale & loadLocale
private void saveLocale(String lang) {
editor.putString("save", lang);
editor.commit();
}
private void loadLocale() {
String lang = pref.getString("save", "");
changeLocale(lang);
}
此鏈接可能會對您有所幫助:https://stackoverflow.com/questions/8049207/how-to-refresh-activity-after-changing-language-locale-inside-application?answertab=oldest#tab-top –
@ Shwe請查看此鏈接---> https://stackoverflow.com/questions/45088039/set-different-language-in-android-studio/45088145#45088145 – yash786