简体   繁体   中英

how to save the get the changes in listpreference?

I want to let the user choose the font size on my app. how can I react to his selections? how can I get the value from his selection? thanks I have this code of the preferences:

<string-array name="aaa_entries">
    <item >0</item>
    <item >1</item>
    <item >2</item>
    <item >3</item>
</string-array>

<string-array name="aaa_values">
    <item >Very small text</item>
    <item >Small text</item>
    <item >Normal text</item>
    <item >Large text</item>
</string-array>

Code:

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    if (key.equals("pref_sync")) {
        String selectedFont=sharedPreferences.getString("pref_sync",null);
        if("Very small text".equalsIgnoreCase(selectedFont)) {
            Toast.makeText(getApplicationContext(),"asdasdasdasd",Toast.LENGTH_SHORT).show();
        }
    }
}

Add a listener to the ListPreference , and when user select an item from the ListPreference , onPreferenceChange will be called with the new value selected.

Preference listPreference = getPreferenceManager().findPreference("your_key");

listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                String userSelectedValue = (String) newValue;
                Toast.makeText(getActivity(), “user picked a font”, Toast.LENGTH_SHORT).show();
                return true;
            }
        }); 

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