下面列出了android.preference.ListPreference#findIndexOfValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
private void setRefreshSummary(String p_refreshInterval) {
ListPreference refreshPref = (ListPreference) findPreference("REFRESH_INTERVAL");
if (refreshPref.findIndexOfValue(p_refreshInterval) >= 0) {
CharSequence listDesc = refreshPref.getEntries()[refreshPref.findIndexOfValue(p_refreshInterval)];
refreshPref.setSummary(listDesc);
} else {
// not in the list (old value?)
int refresh_interval = 0;
try {
refresh_interval = Integer.valueOf(_sharedPreferences.getString("REFRESH_INTERVAL", "500"));
} catch (NumberFormatException nfe) {
refresh_interval = Constants.REFRESH_INTERVAL_DEFAULT;
}
refresh_interval = refresh_interval % 100000;
if (refresh_interval < 1000) {
refreshPref.setSummary(refresh_interval + " ms");
} else {
refreshPref.setSummary(refresh_interval/1000 + " s");
}
}
}
/**
* Update the summary for a preference.
*
* @param preference The preference.
* @param value the new value of the preference.
*/
private void setSummary(@NonNull final Preference preference, final String value) {
if (preference.getClass().equals(ListPreference.class)) {
// For list preferences (except customized ones), look up the correct display value in the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(value);
if (preference.getKey().startsWith(getString(R.string.key_indexed_overlaytype))) {
updateSummaryForOverlayPreference(listPreference);
}
else if (index < 0) {
preference.setSummary(null);
}
else {
String entryValue = listPreference.getEntries()[index].toString();
preference.setSummary(entryValue);
}
}
else {
// For all other preferences, set the summary to the value's simple string representation.
preference.setSummary(value);
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values)
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]);
}
} else {
// For other preferences, set the summary to the value's simple string representation
preference.setSummary(stringValue);
}
return true;
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null
);
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);
} else {
// For all other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
}
return true;
}
/**
* set lowBatPollInterval to normal poll interval at least
* and adapt the selectable values
*
* @param pollIntervalPref
* @param lowBatPollIntervalPref
*/
private void setMinBatPollIntervall(ListPreference pollIntervalPref, ListPreference lowBatPollIntervalPref) {
final String currentValue = lowBatPollIntervalPref.getValue();
final int pollIntervalPos = (pollIntervalPref.findIndexOfValue(pollIntervalPref.getValue()) >= 0?pollIntervalPref.findIndexOfValue(pollIntervalPref.getValue()):0),
length = pollIntervalPref.getEntries().length;
CharSequence[] entries = new String[length - pollIntervalPos],
entryValues = new String[length - pollIntervalPos];
// generate temp Entries and EntryValues
for(int i = pollIntervalPos; i < length; i++) {
entries[i - pollIntervalPos] = pollIntervalPref.getEntries()[i];
entryValues[i - pollIntervalPos] = pollIntervalPref.getEntryValues()[i];
}
lowBatPollIntervalPref.setEntries(entries);
lowBatPollIntervalPref.setEntryValues(entryValues);
// and set the correct one
if (lowBatPollIntervalPref.findIndexOfValue(currentValue) == -1) {
lowBatPollIntervalPref.setValueIndex(0);
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference){
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);
} else if (preference instanceof RingtonePreference) {
} else if (preference instanceof EditTextPreference) {
EditTextPreference editTextPreference = (EditTextPreference) preference;
editTextPreference.setSummary(editTextPreference.getText());
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String stringValue = newValue.toString();
// The method for updating the summary differs based on the preference type
if (preference instanceof TimeIntervalPreference) {
preference.setSummary(((TimeIntervalPreference) preference).getSummary(stringValue));
} else if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);
} else {
preference.setSummary(stringValue);
}
return true;
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
// Damage control if user inputs an empty profile name
if(stringValue.isEmpty() && preference.getKey().equals("profile_name")) {
stringValue = name;
listener.setEmptyTitle(name);
}
if(preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
CharSequence summary = index >= 0 ? listPreference.getEntries()[index] : null;
// Set the summary to reflect the new value.
if(summary == null) {
stringValue = listener.generateBlurb(preference.getKey(), stringValue);
preference.setSummary(stringValue);
} else
preference.setSummary(summary);
} else {
if((preference.getKey().equals("density") && !stringValue.isEmpty())
|| preference.getKey().equals("size"))
stringValue = listener.generateBlurb(preference.getKey(), stringValue);
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
static void updateListPreferenceSummaryToCurrentValue(final String prefKey,
final PreferenceScreen screen) {
// Because the "%s" summary trick of {@link ListPreference} doesn't work properly before
// KitKat, we need to update the summary programmatically.
final ListPreference listPreference = (ListPreference)screen.findPreference(prefKey);
if (listPreference == null) {
return;
}
final CharSequence entries[] = listPreference.getEntries();
final int entryIndex = listPreference.findIndexOfValue(listPreference.getValue());
listPreference.setSummary(entryIndex < 0 ? null : entries[entryIndex]);
}
static void updateListPreferenceSummaryToCurrentValue(final String prefKey,
final PreferenceScreen screen) {
// Because the "%s" summary trick of {@link ListPreference} doesn't work properly before
// KitKat, we need to update the summary programmatically.
final ListPreference listPreference = (ListPreference)screen.findPreference(prefKey);
if (listPreference == null) {
return;
}
final CharSequence entries[] = listPreference.getEntries();
final int entryIndex = listPreference.findIndexOfValue(listPreference.getValue());
listPreference.setSummary(entryIndex < 0 ? null : entries[entryIndex]);
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference
.setSummary(index >= 0 ? listPreference.getEntries()[index]
: null);
} else if (preference instanceof RingtonePreference) {
// For ringtone preferences, look up the correct display value
// using RingtoneManager.
if (TextUtils.isEmpty(stringValue)) {
// Empty values correspond to 'silent' (no ringtone).
preference.setSummary(R.string.pref_ringtone_silent);
} else {
Ringtone ringtone = RingtoneManager.getRingtone(
preference.getContext(), Uri.parse(stringValue));
if (ringtone == null) {
// Clear the summary if there was a lookup error.
preference.setSummary(null);
} else {
// Set the summary to reflect the new ringtone display
// name.
String name = ringtone
.getTitle(preference.getContext());
preference.setSummary(name);
}
}
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else if (preference instanceof RingtonePreference) {
// For ringtone preferences, look up the correct display value
// using RingtoneManager.
if (TextUtils.isEmpty(stringValue)) {
// Empty values correspond to 'silent' (no ringtone).
preference.setSummary(R.string.pref_ringtone_silent);
} else {
Ringtone ringtone = RingtoneManager.getRingtone(
preference.getContext(), Uri.parse(stringValue));
if (ringtone == null) {
// Clear the summary if there was a lookup error.
preference.setSummary(null);
} else {
// Set the summary to reflect the new ringtone display
// name.
String name = ringtone.getTitle(preference.getContext());
preference.setSummary(name);
}
}
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference
.setSummary(index >= 0 ? listPreference.getEntries()[index]
: null);
} else if (preference instanceof RingtonePreference) {
// For ringtone preferences, look up the correct display value
// using RingtoneManager.
if (TextUtils.isEmpty(stringValue)) {
// Empty values correspond to 'silent' (no ringtone).
preference.setSummary(R.string.pref_ringtone_silent);
} else {
Ringtone ringtone = RingtoneManager.getRingtone(
preference.getContext(), Uri.parse(stringValue));
if (ringtone == null) {
// Clear the summary if there was a lookup error.
preference.setSummary(null);
} else {
// Set the summary to reflect the new ringtone display
// name.
String name = ringtone
.getTitle(preference.getContext());
preference.setSummary(name);
}
}
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String preferenceKeyForTriggerModifier =
getKeyComboManager().getKeyComboModel().getPreferenceKeyForTriggerModifier();
if (preference instanceof KeyboardShortcutDialogPreference
&& newValue instanceof Long) {
KeyboardShortcutDialogPreference keyBoardPreference =
(KeyboardShortcutDialogPreference) preference;
keyBoardPreference.setKeyComboCode((Long) newValue);
keyBoardPreference.notifyChanged();
} else if (preference.getKey() != null
&& preference.getKey().equals(getString(R.string.pref_select_keymap_key))
&& newValue instanceof String) {
String newKeymap = (String) newValue;
// Do nothing if keymap is the same.
if (keymap.equals(newKeymap)) {
return false;
}
// Replace preference fragment.
TalkBackKeyboardShortcutPreferenceFragment fragment =
TalkBackKeyboardShortcutPreferenceFragment.createFor(newKeymap);
getFragmentManager()
.beginTransaction()
.replace(android.R.id.content, fragment)
.commit();
// Set new key combo model.
KeyComboManager keyComboManager = getKeyComboManager();
keyComboManager.setKeyComboModel(keyComboManager.createKeyComboModelFor(newKeymap));
// Announce new keymap.
announceText(
String.format(
getString(R.string.keycombo_menu_announce_active_keymap),
getKeymapName(newKeymap)),
getActivity());
} else if (preference.getKey() != null
&& preference.getKey().equals(preferenceKeyForTriggerModifier)
&& newValue instanceof String) {
triggerModifierToBeSet = (String) newValue;
ListPreference listPreference = (ListPreference) preference;
if (listPreference.getValue().equals(triggerModifierToBeSet)) {
return false;
}
CharSequence[] entries = listPreference.getEntries();
CharSequence newTriggerModifier =
entries[listPreference.findIndexOfValue(triggerModifierToBeSet)];
// Show alert dialog.
AlertDialog dialog =
new AlertDialog.Builder(getActivity())
.setTitle(R.string.keycombo_menu_alert_title_trigger_modifier)
.setMessage(
getString(
R.string.keycombo_menu_alert_message_trigger_modifier,
newTriggerModifier))
.setPositiveButton(
android.R.string.ok, chooseTriggerModifierConfirmDialogPositive)
.setNegativeButton(
android.R.string.cancel, chooseTriggerModifierConfirmDialogNegative)
.show();
focusCancelButton(dialog);
return false;
}
return true;
}