使用Preferences [android]在运行时更改语言

时间:2015-07-02 11:44:20

标签: android android-preferences

我为此阅读了几次official guide以及许多其他内容,例如本网站上的问题,但我不能让我的工作100%

目前,如果我更改列表首选项中的语言,活动(SettingsActivity)将不会自动更新(例如,20次中的1次,不知何故,它不知道为什么)。按下后退按钮后,我的MainActivity也没有更新。但是我的所有其他活动总是显示正确的语言,如果我回到SettingsActivity,它将是正常的,与MainActivity相同,如果我重新启动应用程序。

相关部分:

MainActivity:

public class MainActivity extends Activity {

public static final String KEY_PREF_LANGUAGE = "pref_language";

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
}

@Override
protected void onResume() {
    super.onResume();
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);

    String languagePref_ID = sharedPref.getString(KEY_PREF_LANGUAGE, "2");
    switch (languagePref_ID) {
        case "1":
            Locale localeEN = new Locale("en_US");
            setLocale(localeEN);
            break;
        case "2":
            Locale localeHU = new Locale("hu_HU");
            setLocale(localeHU);
            break;

    }
}


public void setLocale(Locale locale) {
    Locale.setDefault(locale);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = locale;
    res.updateConfiguration(conf, dm);
    //recreate();
    //finish();
    //startActivity(getIntent());
    //if these are not commented, main activity wont show at start at all
}

SettingsActivity

public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

public static final String KEY_PREF_LANGUAGE = "pref_language";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new SettingsFragment())
            .commit();
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.OnSharedPreferenceChangeListener listener =
            new SharedPreferences.OnSharedPreferenceChangeListener() {
                public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
                    if (key.equals(KEY_PREF_LANGUAGE)) {
                        String languagePref_ID = prefs.getString(SettingsActivity.KEY_PREF_LANGUAGE, "");
                        switch (languagePref_ID) {
                            case "1":
                                Locale localeEN = new Locale("en_US");
                                setLocale(localeEN);
                                break;
                            case "2":
                                Locale localeHU = new Locale("hu_HU");
                                setLocale(localeHU);
                                break;

                        }
                    }
                }
            };
    sharedPref.registerOnSharedPreferenceChangeListener(listener);
}


public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    if (key.equals(KEY_PREF_LANGUAGE)) {
        String languagePref_ID = sharedPreferences.getString(SettingsActivity.KEY_PREF_LANGUAGE, "2");
        switch (languagePref_ID) {
            case "1":
                Locale localeEN = new Locale("en_US");
                setLocale(localeEN);
                break;
            case "2":
                Locale localeHU = new Locale("hu_HU");
                setLocale(localeHU);
                break;

        }
    }
    //this doenst even get called but i need it for the implementation
}

public void setLocale(Locale locale) {
    Locale.setDefault(locale);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = locale;
    res.updateConfiguration(conf, dm);
    recreate(); //tried like 4 ways to do this, nothing really worked
    //finish();
    //startActivity(getIntent());
}
}

SettingsFragment.java

public class SettingsFragment extends PreferenceFragment {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);

}
}

的AndroidManifest.xml

android:configChanges="locale|orientation" //added to main and settings activity

我对此感到疯狂,这是最好的工作状态,但我知道这里的活动生命管理远非最好,当我尝试这些时,我通常无法让它工作。我如何才能使这项工作真正更新所有活动的运行时间?

2 个答案:

答案 0 :(得分:3)

更改语言后,如果您想要更改语言,则需要重新启动当前活动。例如:

private void restartActivity() {
    Intent intent = getIntent();
    finish();
    startActivity(intent);
}

在这里查看最佳答案How to refresh activity after changing language (Locale) inside application

答案 1 :(得分:1)

最后,我在不使用碎片的情况下使其工作。所以这里有一些不赞成使用的方法,我也试过片段,但还没有完全成功(还)。此代码在更改后始终刷新SettingsActivity,并且MainActivity正确更新自己。

<强> MainActivity

public class MainActivity extends Activity {

public static final String KEY_PREF_LANGUAGE = "pref_language";
public String languagePref_ID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    languagePref_ID = sharedPref.getString(KEY_PREF_LANGUAGE, "2");
    switch (languagePref_ID) {
        case "1":
            Locale localeEN = new Locale("en_US");
            setLocaleOnCreate(localeEN);
            break;
        case "2":
            Locale localeHU = new Locale("hu_HU");
            setLocaleOnCreate(localeHU);
            break;

    }
    setContentView(R.layout.activity_main);
    ....
}
@Override
protected void onResume() {
    super.onResume();
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    String languagePref_ID_RES = sharedPref.getString(KEY_PREF_LANGUAGE, "2");
    if(!languagePref_ID.equals(languagePref_ID_RES)){
        languagePref_ID_RES = languagePref_ID;
        switch (languagePref_ID_RES) {
            case "1":
                Locale localeEN = new Locale("en_US");
                setLocale(localeEN);
                break;
            case "2":
                Locale localeHU = new Locale("hu_HU");
                setLocale(localeHU);
                break;

        }
    }
}
    public void setLocaleOnCreate(Locale locale) {
    Locale.setDefault(locale);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = locale;
    res.updateConfiguration(conf, dm);
}

public void setLocale(Locale locale) {
    Locale.setDefault(locale);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = locale;
    res.updateConfiguration(conf, dm);
    recreate();
    //finish();
    //startActivity(getIntent());
}
  ....

    public void startSettingsActivity(View view) {
    Intent intent = new Intent(this, SettingsActivity.class);
    startActivity(intent);
  }
}

<强> SettingsActivity

public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

public static final String KEY_PREF_LANGUAGE = "pref_language";
public String languagePref_ID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.OnSharedPreferenceChangeListener listener =
            new SharedPreferences.OnSharedPreferenceChangeListener() {
                public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
                    if (key.equals(KEY_PREF_LANGUAGE)) {
                        languagePref_ID = prefs.getString(SettingsActivity.KEY_PREF_LANGUAGE, "2");
                        switch (languagePref_ID) {
                            case "1":
                                Locale localeEN = new Locale("en_US");
                                setLocale(localeEN);
                                break;
                            case "2":
                                Locale localeHU = new Locale("hu_HU");
                                setLocale(localeHU);
                                break;
                        }
                    }
                }
            };
    sharedPref.registerOnSharedPreferenceChangeListener(listener);
    addPreferencesFromResource(R.xml.preferences);
}

@Override
protected void onResume() {
    super.onResume();
    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}

@Override
protected void onPause() {
    super.onPause();
    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    if (key.equals(KEY_PREF_LANGUAGE)) {
        languagePref_ID = sharedPreferences.getString(SettingsActivity.KEY_PREF_LANGUAGE, "2");
        switch (languagePref_ID) {
            case "1":
                Locale localeEN = new Locale("en_US");
                setLocale(localeEN);
                break;
            case "2":
                Locale localeHU = new Locale("hu_HU");
                setLocale(localeHU);
                break;
        }
    }
}

public void setLocale(Locale locale) {
    Locale.setDefault(locale);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = locale;
    res.updateConfiguration(conf, dm);
    recreate();
 }
}