如何在重新启动时使用SharedPreferences在按钮上保存用户的语言选择

时间:2017-11-11 03:06:02

标签: java android button sharedpreferences

我将语言保存在共享偏好中并且似乎已成功保存,但是当我重新打开应用程序时,它会恢复为默认语言“en”,如何使用之前保存和选择的相同语言启动活动,有两个按钮,每个按钮用不同的语言,英语和阿拉伯语重新启动活动。

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);
}

}

1 个答案:

答案 0 :(得分: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);
    }
}
希望它可以帮到你。