2017-11-11 195 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 –

相关问题