Change the summary of a ListPreference with the new value (Android)

前端 未结 14 1134
感动是毒
感动是毒 2020-12-30 03:01

How can I modify the summary of a ListPreference to the new \"Entry\" string selected by the user (not the entry value)

I suppouse its with setOnPreferenceChangeList

14条回答
  •  Happy的楠姐
    2020-12-30 03:54

    Referring to the accepted answer ( @Michael ), if you modify the MyListPreference to add an onPreferenceChangeListener:

    public MyListPreference(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        setOnPreferenceChangeListener(this);
    }
    

    and have the listener implementation return true:

    public boolean onPreferenceChange(Preference preference, Object newValue) {
        // Do other stuff as needed, e.g. setTitle()
        return true;
    }
    

    the summary '%s' always resolves to the latest setting. Otherwise, it always has the default setting.

    Don't forget to add implements:

    public class MyListPreference extends ListPreference implements Preference.OnPreferenceChangeListener
    

提交回复
热议问题