下面列出了android.preference.ListPreference#setValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void init() {
final ListPreference theme = (ListPreference) findPreference("preference_theme");
theme.setEntryValues(THEMES_VALUES);
theme.setValue(String.valueOf(Settings.getDefaultTheme()));
theme.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final int chosenTheme = Integer.parseInt((String) newValue);
if (chosenTheme != Settings.getDefaultTheme()) {
Settings.setDefaultTheme(chosenTheme);
((SettingsActivity) getActivity()).proxyRestart();
return true;
}
return false;
}
});
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
setHasOptionsMenu(true);
ListPreference speedListPreference = (ListPreference) findPreference("min_speed");
ListPreference effListPreference = (ListPreference) findPreference("default_efficiency");
//Statistics.setDefault_efficiency(Integer.valueOf(String.valueOf(effListPreference.getValue())));
//Statistics.setMin_speed(Integer.valueOf(String.valueOf(speedListPreference.getValue())));
speedListPreference.setValueIndex(3);
speedListPreference.setValue("4");
effListPreference.setValueIndex(7);
effListPreference.setValue("600");
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
bindPreferenceSummaryToValue(findPreference("min_speed"));
bindPreferenceSummaryToValue(findPreference("default_efficiency"));
}
private void setText(String key, String value) {
Preference p = findAnyPreference(key, null);
if (p instanceof EditTextPreference) {
EditTextPreference etp = (EditTextPreference) p;
etp.setText(value);
// show value as summary
etp.setOnPreferenceChangeListener((Preference preference, Object newValue) -> {
preference.setSummary(getSummaryValue(key, newValue.toString()));
return true;
});
etp.setSummary(getSummaryValue(key, value));
} else if (p instanceof ListPreference) {
ListPreference lp = (ListPreference) p;
lp.setValue(value);
// set summary field to "%s" in xml
} else {
Log.w("SetupActivity.setText", "Cannot find EditTextPreference/ListPreference in PreferenceGroup with key: " + key);
}
}
private void setRpcServerPreferenceData(ListPreference lp) {
NetworkInfo[] networks = ethereumNetworkRepository.getAvailableNetworkList();
CharSequence[] entries = new CharSequence[networks.length];
for (int ii = 0; ii < networks.length; ii++) {
entries[ii] = networks[ii].name;
}
CharSequence[] entryValues = new CharSequence[networks.length];
for (int ii = 0; ii < networks.length; ii++) {
entryValues[ii] = networks[ii].name;
}
String currentValue = ethereumNetworkRepository.getDefaultNetwork().name;
lp.setEntries(entries);
lp.setDefaultValue(currentValue);
lp.setValue(currentValue);
lp.setSummary(currentValue);
lp.setEntryValues(entryValues);
}
private void initializePreferredVideoFpsPreferences(ListPreference pref) {
List<CharSequence> entries = new ArrayList<CharSequence>();
List<CharSequence> values = new ArrayList<CharSequence>();
entries.add(getString(R.string.pref_none));
values.add("0");
for (int i = 5; i <= 30; i += 5) {
String str = Integer.toString(i);
entries.add(str);
values.add(str);
}
setListPreferenceValues(pref, entries, values);
String value = Integer.toString(mPrefs.getPreferredVideoFps());
if (value.equals("0")) {
value = getString(R.string.pref_none);
}
pref.setSummary(value);
pref.setValue(value);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
boolean hasReminders = accountManager.getAllowReminders(getActivity());
((SwitchPreference) findPreference(AccountManager.KEY_ALLOW_REMINDERS))
.setChecked(hasReminders);
Set<String> reminderDays = accountManager.getReminderDays(getActivity());
MultiSelectListPreference daysPreference =
(MultiSelectListPreference) findPreference(AccountManager.KEY_REMINDER_DAYS);
daysPreference.setValues(reminderDays);
updateReminderDaysSummary(daysPreference, reminderDays);
String notificationSetting = accountManager.getNotificationPreference(getActivity());
ListPreference notificationPref =
(ListPreference) findPreference(AccountManager.KEY_NOTIFICATIONS);
notificationPref.setValue(notificationSetting);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ListPreference urlPreference = (ListPreference) findPreference(getString(R.string.settings_url_key));
if (urlPreference.getValue() == null) {
urlPreference.setValue(getString(BuildConfig.MEATSPACE_BASE_URL));
}
urlPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
getActivity().sendBroadcast(new Intent(Constants.FILTER_CHAT_CLOSE));
return true;
}
});
}
private void initTunnelSettings() {
if (!LinphoneManager.getLc().isTunnelAvailable()) {
return;
}
setPreferenceDefaultValueAndSummary(R.string.pref_tunnel_host_key, mPrefs.getTunnelHost());
setPreferenceDefaultValueAndSummary(R.string.pref_tunnel_port_key, String.valueOf(mPrefs.getTunnelPort()));
ListPreference tunnelModePref = (ListPreference) findPreference(getString(R.string.pref_tunnel_mode_key));
String tunnelMode = mPrefs.getTunnelMode();
tunnelModePref.setSummary(tunnelMode);
tunnelModePref.setValue(tunnelMode);
}
/**
* {@inheritDoc}
*/
@Override
public void setValue(@NonNull Preference preference,
@NonNull ConfigBase.Option option,
@NonNull Object value) {
ListPreference cbp = (ListPreference) preference;
cbp.setValue(Integer.toString((Integer) value));
}
private void initializePreferredVideoSizePreferences(ListPreference pref) {
List<CharSequence> entries = new ArrayList<CharSequence>();
List<CharSequence> values = new ArrayList<CharSequence>();
for (String name : LinphoneManager.getLc().getSupportedVideoSizes()) {
entries.add(name);
values.add(name);
}
setListPreferenceValues(pref, entries, values);
String value = mPrefs.getPreferredVideoSize();
pref.setSummary(value);
pref.setValue(value);
}
private void initLimeEncryptionPreference(ListPreference pref) {
List<CharSequence> entries = new ArrayList<CharSequence>();
List<CharSequence> values = new ArrayList<CharSequence>();
entries.add(getString(R.string.lime_encryption_entry_disabled));
values.add(LinphoneLimeState.Disabled.toString());
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
if (lc == null || !lc.isLimeEncryptionAvailable()) {
setListPreferenceValues(pref, entries, values);
pref.setEnabled(false);
return;
}
entries.add(getString(R.string.lime_encryption_entry_mandatory));
values.add(LinphoneLimeState.Mandatory.toString());
entries.add(getString(R.string.lime_encryption_entry_preferred));
values.add(LinphoneLimeState.Preferred.toString());
setListPreferenceValues(pref, entries, values);
LinphoneLimeState lime = mPrefs.getLimeEncryption();
if (lime == LinphoneLimeState.Disabled) {
pref.setSummary(getString(R.string.lime_encryption_entry_disabled));
} else if (lime == LinphoneLimeState.Mandatory) {
pref.setSummary(getString(R.string.lime_encryption_entry_mandatory));
} else if (lime == LinphoneLimeState.Preferred) {
pref.setSummary(getString(R.string.lime_encryption_entry_preferred));
}
pref.setValue(lime.toString());
}
public void setPageAlign(final PageAnimationType type, final String alignPrefKey) {
if (type != null && type != PageAnimationType.NONE) {
final ListPreference alignPref = (ListPreference) findPreference(alignPrefKey);
alignPref.setValue(PageAlign.AUTO.getResValue());
setListPreferenceSummary(alignPref, alignPref.getValue());
}
}
public void createOutputPortPreferences() {
String currentValue = null;
String currentLabel = null;
final List<String> entryNames = new ArrayList<String>();
final List<String> entryValues = new ArrayList<String>();
MidiPlayer midiPlayer = MidiPlayer.getInstance(this.findContext());
List<MidiOutputPort> outputPorts = midiPlayer.listOutputPorts();
for(MidiOutputPort outputPort : outputPorts) {
entryNames.add(outputPort.getName());
entryValues.add(outputPort.getKey());
if( midiPlayer.isOutputPortOpen(outputPort.getKey()) ) {
currentValue = outputPort.getKey();
currentLabel = outputPort.getName();
}
}
final ListPreference listPreference = (ListPreference) this.findPreference(TGTransportProperties.PROPERTY_MIDI_OUTPUT_PORT);
listPreference.setEntries(entryNames.toArray(new CharSequence[entryNames.size()]));
listPreference.setEntryValues(entryValues.toArray(new CharSequence[entryValues.size()]));
if( currentValue != null ) {
listPreference.setValue(currentValue);
}
listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object o) {
int index = ( o != null ? entryValues.indexOf(o.toString()) : -1);
String selectedLabel = ( index >= 0 ? entryNames.get(index) : null);
updatePreferenceSummary(preference, selectedLabel, R.string.preferences_midi_output_port_summary, R.string.preferences_midi_output_port_summary_empty);
return true;
}
});
updatePreferenceSummary(listPreference, currentLabel, R.string.preferences_midi_output_port_summary, R.string.preferences_midi_output_port_summary_empty);
}
@Override
public void onClick(DialogInterface dialogInterface, int i) {
resetKeymap();
KeyComboModel keyComboModel = getKeyComboManager().getKeyComboModel();
// Update preference.
String preferenceKeyForTriggerModifier =
keyComboModel.getPreferenceKeyForTriggerModifier();
ListPreference listPreference =
(ListPreference) findPreference(preferenceKeyForTriggerModifier);
listPreference.setValue(triggerModifierToBeSet);
// Update KeyComboModel.
keyComboModel.notifyTriggerModifierChanged();
// Update UI.
Set<String> keySet =
getKeyComboManager().getKeyComboModel().getKeyComboCodeMap().keySet();
for (String key : keySet) {
KeyboardShortcutDialogPreference preference =
(KeyboardShortcutDialogPreference) findPreference(key);
preference.onTriggerModifierChanged();
}
// Announce that trigger modifier has changed.
CharSequence[] entries = listPreference.getEntries();
CharSequence newTriggerModifier =
entries[listPreference.findIndexOfValue(triggerModifierToBeSet)];
announceText(
getString(R.string.keycombo_menu_announce_new_trigger_modifier, newTriggerModifier),
getActivity());
triggerModifierToBeSet = null;
}
@Override
protected void onResume() {
super.onResume();
sharedPreferences.registerOnSharedPreferenceChangeListener(this.sharedPreferenceChangeListener);
if (needUpdateChansScreen) {
updateChansScreen((PreferenceScreen) getPreferenceManager().findPreference(getString(R.string.pref_key_cat_chans)));
}
ListPreference themePreference = ((ListPreference) getPreferenceManager().findPreference(getString(R.string.pref_key_theme)));
String currentValue = sharedPreferences.getString(getString(R.string.pref_key_theme), "");
if (!currentValue.equals("") && !currentValue.equals(themePreference.getValue())) {
themePreference.setValue(currentValue);
updateListSummary(R.string.pref_key_theme);
}
}
/**
* {@inheritDoc}
*/
@Override
public void setValue(@NonNull Preference preference,
@NonNull ConfigBase.Option option,
@NonNull Object value) {
ListPreference cbp = (ListPreference) preference;
cbp.setValue(Integer.toString((Integer) value));
}
@Override
public void onCreate(final Bundle icicle) {
super.onCreate(icicle);
addPreferencesFromResource(R.xml.prefs_screen_advanced);
final Resources res = getResources();
final Context context = getActivity();
// When we are called from the Settings application but we are not already running, some
// singleton and utility classes may not have been initialized. We have to call
// initialization method of these classes here. See {@link LatinIME#onCreate()}.
AudioAndHapticFeedbackManager.init(context);
final SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
if (!Settings.isInternal(prefs)) {
removePreference(Settings.SCREEN_DEBUG);
}
if (!AudioAndHapticFeedbackManager.getInstance().hasVibrator()) {
removePreference(Settings.PREF_VIBRATION_DURATION_SETTINGS);
}
// TODO: consolidate key preview dismiss delay with the key preview animation parameters.
if (!Settings.readFromBuildConfigIfToShowKeyPreviewPopupOption(res)) {
removePreference(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY);
} else {
// TODO: Cleanup this setup.
final ListPreference keyPreviewPopupDismissDelay =
(ListPreference) findPreference(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY);
final String popupDismissDelayDefaultValue = Integer.toString(res.getInteger(
R.integer.config_key_preview_linger_timeout));
keyPreviewPopupDismissDelay.setEntries(new String[] {
res.getString(R.string.key_preview_popup_dismiss_no_delay),
res.getString(R.string.key_preview_popup_dismiss_default_delay),
});
keyPreviewPopupDismissDelay.setEntryValues(new String[] {
"0",
popupDismissDelayDefaultValue
});
if (null == keyPreviewPopupDismissDelay.getValue()) {
keyPreviewPopupDismissDelay.setValue(popupDismissDelayDefaultValue);
}
keyPreviewPopupDismissDelay.setEnabled(
Settings.readKeyPreviewPopupEnabled(prefs, res));
}
setupKeypressVibrationDurationSettings();
setupKeypressSoundVolumeSettings();
setupKeyLongpressTimeoutSettings();
refreshEnablingsOfKeypressSoundAndVibrationSettings();
}
@Override
public void onCreate(final Bundle icicle) {
super.onCreate(icicle);
addPreferencesFromResource(R.xml.prefs_screen_advanced);
final Resources res = getResources();
final Context context = getActivity();
// When we are called from the Settings application but we are not already running, some
// singleton and utility classes may not have been initialized. We have to call
// initialization method of these classes here. See {@link LatinIME#onCreate()}.
AudioAndHapticFeedbackManager.init(context);
final SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
if (!Settings.isInternal(prefs)) {
removePreference(Settings.SCREEN_DEBUG);
}
if (!AudioAndHapticFeedbackManager.getInstance().hasVibrator()) {
removePreference(Settings.PREF_VIBRATION_DURATION_SETTINGS);
}
// TODO: consolidate key preview dismiss delay with the key preview animation parameters.
if (!Settings.readFromBuildConfigIfToShowKeyPreviewPopupOption(res)) {
removePreference(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY);
} else {
// TODO: Cleanup this setup.
final ListPreference keyPreviewPopupDismissDelay =
(ListPreference) findPreference(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY);
final String popupDismissDelayDefaultValue = Integer.toString(res.getInteger(
R.integer.config_key_preview_linger_timeout));
keyPreviewPopupDismissDelay.setEntries(new String[] {
res.getString(R.string.key_preview_popup_dismiss_no_delay),
res.getString(R.string.key_preview_popup_dismiss_default_delay),
});
keyPreviewPopupDismissDelay.setEntryValues(new String[] {
"0",
popupDismissDelayDefaultValue
});
if (null == keyPreviewPopupDismissDelay.getValue()) {
keyPreviewPopupDismissDelay.setValue(popupDismissDelayDefaultValue);
}
keyPreviewPopupDismissDelay.setEnabled(
Settings.readKeyPreviewPopupEnabled(prefs, res));
}
setupKeypressVibrationDurationSettings();
setupKeypressSoundVolumeSettings();
setupKeyLongpressTimeoutSettings();
refreshEnablingsOfKeypressSoundAndVibrationSettings();
}
@Override
public void onCreate(final Bundle icicle) {
super.onCreate(icicle);
addPreferencesFromResource(R.xml.prefs_screen_advanced);
final Resources res = getResources();
final Context context = getActivity();
// When we are called from the Settings application but we are not already running, some
// singleton and utility classes may not have been initialized. We have to call
// initialization method of these classes here. See {@link LatinIME#onCreate()}.
AudioAndHapticFeedbackManager.init(context);
final SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
if (!Settings.isInternal(prefs)) {
removePreference(Settings.SCREEN_DEBUG);
}
if (!AudioAndHapticFeedbackManager.getInstance().hasVibrator()) {
removePreference(Settings.PREF_VIBRATION_DURATION_SETTINGS);
}
// TODO: consolidate key preview dismiss delay with the key preview animation parameters.
if (!Settings.readFromBuildConfigIfToShowKeyPreviewPopupOption(res)) {
removePreference(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY);
} else {
// TODO: Cleanup this setup.
final ListPreference keyPreviewPopupDismissDelay =
(ListPreference) findPreference(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY);
final String popupDismissDelayDefaultValue = Integer.toString(res.getInteger(
R.integer.config_key_preview_linger_timeout));
keyPreviewPopupDismissDelay.setEntries(new String[] {
res.getString(R.string.key_preview_popup_dismiss_no_delay),
res.getString(R.string.key_preview_popup_dismiss_default_delay),
});
keyPreviewPopupDismissDelay.setEntryValues(new String[] {
"0",
popupDismissDelayDefaultValue
});
if (null == keyPreviewPopupDismissDelay.getValue()) {
keyPreviewPopupDismissDelay.setValue(popupDismissDelayDefaultValue);
}
keyPreviewPopupDismissDelay.setEnabled(
Settings.readKeyPreviewPopupEnabled(prefs, res));
}
setupKeypressVibrationDurationSettings();
setupKeypressSoundVolumeSettings();
setupKeyLongpressTimeoutSettings();
refreshEnablingsOfKeypressSoundAndVibrationSettings();
}
@SuppressWarnings("deprecation")
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(key.contains("notification_action")) {
String value = sharedPreferences.getString(key, "null");
if(isUnsupported(value)) {
switch(key) {
case "notification_action":
value = lastValue;
break;
case "notification_action_2":
value = lastValue2;
break;
}
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.apply();
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) findPreference(key);
int index = listPreference.findIndexOfValue(value);
// Set the summary to reflect the new value.
listPreference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);
listPreference.setValue(value);
U.showToast(this, R.string.not_compatible);
} else {
switch(key) {
case "notification_action":
lastValue = value;
break;
case "notification_action_2":
lastValue2 = value;
break;
}
}
}
}