How to Get Selected Text and Value Android ListPreference

前端 未结 5 1194
清歌不尽
清歌不尽 2020-12-10 01:23

The XML file of my ListPreference



        
5条回答
  •  庸人自扰
    2020-12-10 02:07

    Here is an example:

    @Override
    public boolean onPreferenceChange(Preference preference, Object value)
    {
        String textValue = value.toString();
    
        ListPreference listPreference = (ListPreference) preference;
        int index = listPreference.findIndexOfValue(textValue);
    
        CharSequence[] entries = listPreference.getEntries();
    
        if(index >= 0)
            Toast.makeText(preference.getContext(), entries[index], Toast.LENGTH_LONG);
    
        return true;
    }
    
    • index contains the index of the clicked item
    • textValue is the Selected Value
    • entries[index] is the Selected Text

提交回复
热议问题