简体   繁体   中英

Android ListPreference

I am implementing run time language changing for a game. Saving data in preferences. Everything works fine except one thing:

In preferences I specify languages in ListPreference. if I change language - it is excellent changed everywhere in the app, but if I try to change language again, in ListPreference pointer is always on the English (default value) instead of current language. Once again: default value is English. I change it to French. My app language becomes French, but when I enter ListPreference now - the pointer is still on the English (like if English is current) , but has to be on French.

Here is my code:

preferences.xml

    PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

        <PreferenceCategory
           <ListPreference
                android:key="language"
                android:title="@string/language"
                android:summary="@string/select_language"
                android:defaultValue="en"
                android:entries="@array/languages"
                android:entryValues="@array/languagesValues"
                />

    </PreferenceScreen>

arrays .xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>

        <string-array name="languages">
            <item name="english">English</item>
            <item name="french">French</item>
        </string-array>

        <string-array name="languagesValues">
            <item name="english">en</item>
            <item name="french">fr</item>
        </string-array>

    </resources>

PreferenceActivity

        public class Settings extends PreferenceActivity {

            Locale myLocale;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);

            Preference langPreference = getPreferenceScreen().findPreference(
                    "language");
            langPreference.setOnPreferenceChangeListener(languageChangeListener);
        }

        Preference.OnPreferenceChangeListener languageChangeListener = new OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {

                switch (newValue.toString()) {
                case "en":
                    setLocale("en");
                     Toast.makeText(getApplicationContext(), "Locale in English!", Toast.LENGTH_LONG).show();
                    break;

                case "fr":
                    setLocale("fr");
                     Toast.makeText(getApplicationContext(), "Locale in French!", Toast.LENGTH_LONG).show();
                    break;
                }
                return false;
            }
        };

        //* manually changing current locale/
        public void setLocale(String lang) { 
            myLocale = new Locale(lang); 
            Resources res = getResources(); 
            DisplayMetrics dm = res.getDisplayMetrics(); 
            Configuration conf = res.getConfiguration(); 
            conf.locale = myLocale; 
            res.updateConfiguration(conf, dm); 
            Intent refresh = new Intent(this, Settings.class); 
            finish();
            startActivity(refresh); 
        } 

    }

You are returning false instead of true . onPreferenceChange documentation: returns "True to update the state of the Preference with the new value."

Because you are returning false, you are not updating the state of the Preference .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM