2017-09-10 64 views
0

我在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); 
} 
+0

此鏈接可能會對您有所幫助:https://stackoverflow.com/questions/8049207/how-to-refresh-activity-after-changing-language-locale-inside-application?answertab=oldest#tab-top –

+0

@ Shwe請查看此鏈接---> https://stackoverflow.com/questions/45088039/set-different-language-in-android-studio/45088145#45088145 – yash786

回答

0

您可以關閉和打開相同的活動,如:

startActivity(new Intent(ActivityName.this, ActivityName.class)); 
finish(); 

我希望這個作品。

0
/***You need to create separate String file for both arabic and 
English***/ 

English 
Values 
String.xml 
Hindi 
Values-hi 
String.xml 

/**You can change the application language using the following code:**/ 

language = "en"; or language = "hi"; 

@Override 
public void recreate() 
{ 
if (android.os.Build.VERSION.SDK_INT >= 14) 
{ 
    super.recreate(); 
} 
else 
{ 
    startActivity(getIntent()); 
    finish(); 
} 
} 

private void updateLanguage(String language) 
{ 
Locale locale = new Locale(language); 
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",language); 
editor.apply(); 

recreate(); 
} 
0

使用recreate()重新啓動當前活動。