2017-11-11 55 views
0

我將該語言保存在sharedpreference中,似乎已成功保存,但是當我重新打開應用程序時,它將恢復爲默認語言「en」,如何使該活動以保存和選擇的同一種語言開始,我有兩個按鈕,每一個都以不同的語言,英語和阿拉伯語重新開始活動。如何在重新啓動時使用SharedPreferences在按鈕上保存用戶的語言選擇

import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.res.Configuration; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.Spinner; 
import android.widget.Toast; 

import java.util.Locale; 

public class Languages extends AppCompatActivity { 
SharedPreferences prefs; 
String languageToLoad; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_languages); 
    prefs = getSharedPreferences("Language", MODE_PRIVATE); 
    String L = prefs.getString("Language", ""); 
    languageToLoad = L; 





} 


public void language_en(View view) { 
    languageToLoad = "en"; 
    SharedPreferences.Editor editor = prefs.edit(); 
    editor.putString("Language", languageToLoad); 
    editor.commit(); 
    Locale locale = new Locale(String.valueOf(languageToLoad)); 
    Locale.setDefault(locale); 
    Configuration config = new Configuration(); 
    config.locale = locale; 
    Languages.this.getResources().updateConfiguration(config, Languages.this.getResources().getDisplayMetrics()); 
    Intent intent = new Intent(Languages.this, MainActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    startActivity(intent); 
} 
public void language_ar(View view) { 
    languageToLoad = "ar"; 
    SharedPreferences.Editor editor = prefs.edit(); 
    editor.putString("Language", languageToLoad); 
    editor.commit(); 
    Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show(); 
    Locale locale = new Locale(languageToLoad); 
    Locale.setDefault(locale); 
    Configuration config = new Configuration(); 
    config.locale = locale; 
    Languages.this.getResources().updateConfiguration(config, Languages.this.getResources().getDisplayMetrics()); 

    Intent intent = new Intent(Languages.this, MainActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    startActivity(intent); 
} 

}

+0

在setContentView(R.layout.activity_languages)之前,調用您的語言設置方法.. – user3678528

+0

您的代碼格式不正確,請更正格式 – Ibo

回答

0
import android.app.Activity; 
    import android.content.Intent; 
    import android.content.SharedPreferences; 
    import android.content.res.Configuration; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.AdapterView; 
    import android.widget.Spinner; 
    import android.widget.Toast; 

    import java.util.Locale; 

    public class Languages extends AppCompatActivity { 
    SharedPreferences prefs; 
    String languageToLoad; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     updateLanguage(this); 
     setContentView(R.layout.activity_languages); 

    } 

    public void updateLanguage(Context context) 
    { 
     prefs = getSharedPreferences("Language", MODE_PRIVATE); 
     String L = prefs.getString("Language", ""); 
     languageToLoad = L; 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
     Resources res = context.getResources(); 
     DisplayMetrics dm = res.getDisplayMetrics(); 
     Configuration conf = res.getConfiguration(); 
     conf.setLocale(new Locale(languageToLoad)); 
     res.updateConfiguration(conf, dm); 
     } else { 
     Locale locale = new Locale(languageToLoad); 
     Locale.setDefault(locale); 
     Configuration config = new Configuration(); 
     config.locale = locale; 
     context.getResources().updateConfiguration(config, 
       context.getResources().getDisplayMetrics()); 
     } 
    } 


    public void language_en(View view) { 
     languageToLoad = "en"; 
     SharedPreferences.Editor editor = prefs.edit(); 
     editor.putString("Language", languageToLoad); 
     editor.commit(); 

     Intent intent = new Intent(Languages.this, MainActivity.class); 
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     startActivity(intent); 
    } 
    public void language_ar(View view) { 
     languageToLoad = "ar"; 
     SharedPreferences.Editor editor = prefs.edit(); 
     editor.putString("Language", languageToLoad); 
     editor.commit(); 
     Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show(); 


     Intent intent = new Intent(Languages.this, MainActivity.class); 
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     startActivity(intent); 
    } 
} 

希望它可以幫助你。

+0

它仍然保存語言,但是當我重新啓動應用程序時,它無法打開與選擇的語言。 –

+0

@HadyElHawary我已經更新了答案再試一次。 –

+0

將'updateLanguage()'移動到某個全局位置,在開始任何活動或片段之前先調用'updateLanguage()'發送給lanaguge –

相關問題