Domanda

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();
        }
    }
}
È stato utile?

Soluzione

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;
            }
        }); 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top