下面列出了android.preference.EditTextPreference#setSummary ( ) 实例代码,或者点击链接到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 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;
}
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 setSummaries() {
final EditTextPreference urlPreference = (EditTextPreference) findPreference(PREF_SERVER_URL);
urlPreference.setSummary(preferences.getString(PREF_SERVER_URL, this.getResources().getString(R.string.server_url_summary)));
final String serverPassHash = preferences.getString(PREF_SERVER_PASS_HASH, "");
final EditTextPreference serverPassTextPreference = (EditTextPreference) findPreference(PREF_SERVER_PASS);
if (serverPassHash.isEmpty()) {
serverPassTextPreference.setSummary(getResources().getString(R.string.server_password_summary));
} else {
serverPassTextPreference.setSummary(getResources().getString(R.string.server_password_summary_set));
}
final EditTextPreference httpLoginPreference = (EditTextPreference) findPreference(PREF_SERVER_HTTP_AUTH_LOGIN);
httpLoginPreference.setSummary(preferences.getString(PREF_SERVER_HTTP_AUTH_LOGIN, ""));
final String httpPass = preferences.getString(PREF_SERVER_HTTP_AUTH_PASS,"");
if (!httpPass.isEmpty()) {
final EditTextPreference httpPassPreference = (EditTextPreference) findPreference(PREF_SERVER_HTTP_AUTH_PASS);
httpPassPreference.setSummary(getResources().getString(R.string.server_password_summary_set));
}
}
@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 void addPreferencesOnScreen(PreferenceGroup preferenceGroup) {
Context context = preferenceGroup.getContext();
addPasswordPreference(preferenceGroup);
CheckBoxPreference onionPref = new LazyPreferences.CheckBoxPreference(context);
onionPref.setTitle(R.string.pref_use_onion);
onionPref.setSummary(R.string.pref_use_onion_summary);
onionPref.setKey(getSharedKey(PREF_KEY_USE_ONION));
onionPref.setDefaultValue(false);
onionPref.setDisableDependentsState(true);
preferenceGroup.addPreference(onionPref);
EditTextPreference domainPref = new EditTextPreference(context);
domainPref.setTitle(R.string.pref_domain);
domainPref.setDialogTitle(R.string.pref_domain);
domainPref.setSummary(resources.getString(R.string.pref_domain_summary, DOMAINS_HINT));
domainPref.setKey(getSharedKey(PREF_KEY_DOMAIN));
domainPref.getEditText().setHint(DEFAULT_DOMAIN);
domainPref.getEditText().setSingleLine();
domainPref.getEditText().setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI);
preferenceGroup.addPreference(domainPref);
domainPref.setDependency(getSharedKey(PREF_KEY_USE_ONION));
addProxyPreferences(preferenceGroup);
}
/**
* Добавить в группу параметров (на экран/в категорию) параметр задания пароля для удаления постов/файлов
* @param group группа, на которую добавляется параметр
*/
protected void addPasswordPreference(PreferenceGroup group) {
final Context context = group.getContext();
EditTextPreference passwordPref = new EditTextPreference(context) {
@Override
protected void showDialog(Bundle state) {
if (createPassword()) {
setText(getDefaultPassword());
}
super.showDialog(state);
}
};
passwordPref.setTitle(R.string.pref_password_title);
passwordPref.setDialogTitle(R.string.pref_password_title);
passwordPref.setSummary(R.string.pref_password_summary);
passwordPref.setKey(getSharedKey(PREF_KEY_PASSWORD));
passwordPref.getEditText().setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
passwordPref.getEditText().setSingleLine();
passwordPref.getEditText().setFilters(new InputFilter[] { new InputFilter.LengthFilter(255) });
group.addPreference(passwordPref);
}
private void loadGroup() {
// Consider adding more features here, such as those in Track and TrackPreferenceFragment
name = (EditTextPreference) findPreference("name");
name.setText(group.getName());
name.setSummary(group.getName());
description = (EditTextPreference) findPreference("description");
description.setText(group.getDescription());
description.setSummary(group.getDescription());
mTracksPref = (MultiSelectListPreference) findPreference("tracks");
mTracksPref.setValues(getTrackIdsForGroupAsSet(group.getId()));
mTracksPref.setEntries(getAllTrackNamesAsCharSeq());
mTracksPref.setEntryValues(getAllTrackIdsAsCharSeq());
mTracksPref.setSummary(getTrackNamesForSummary());
}
public void addKompturcodePreference(PreferenceGroup preferenceGroup) {
Context context = preferenceGroup.getContext();
EditTextPreference kompturcodePreference = new EditTextPreference(context);
kompturcodePreference.setTitle(R.string.kraut_prefs_kompturcode);
kompturcodePreference.setDialogTitle(R.string.kraut_prefs_kompturcode);
kompturcodePreference.setSummary(R.string.kraut_prefs_kompturcode_summary);
kompturcodePreference.setKey(getSharedKey(PREF_KEY_KOMPTURCODE_COOKIE));
kompturcodePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
setKompturcodeCookie((String) newValue);
return true;
}
});
preferenceGroup.addPreference(kompturcodePreference);
}
@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;
}
/**
* Create an EditTextPreference for the specified preference
* @param titleResId resource ID to use for the title
* @param key preference key
* @return newly created preference
*/
private EditTextPreference createEditTextPreference(@StringRes int titleResId, Settings.Key key) {
final EditTextPreference editTextPreference = new EditTextPreference(getActivity());
editTextPreference.setDefaultValue(mSettings.getDefault(key));
editTextPreference.setKey(key.name());
editTextPreference.setSummary(mSettings.getString(key));
editTextPreference.setTitle(titleResId);
editTextPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
editTextPreference.setSummary((String) newValue);
return true;
}
});
return editTextPreference;
}
private static void adjustUnitDependentPrefs(Preference pref) {
// convert preferences values to current units
String[] unitDependent = new String[]{
MainApp.gs(R.string.key_hypo_target),
MainApp.gs(R.string.key_activity_target),
MainApp.gs(R.string.key_eatingsoon_target),
MainApp.gs(R.string.key_high_mark),
MainApp.gs(R.string.key_low_mark)
};
if (Arrays.asList(unitDependent).contains(pref.getKey())) {
EditTextPreference editTextPref = (EditTextPreference) pref;
String converted = Profile.toCurrentUnitsString(SafeParse.stringToDouble(editTextPref.getText()));
editTextPref.setSummary(converted);
editTextPref.setText(converted);
}
}
/**
* Show IP Address.
*/
private void showIPAddress() {
String ipAddress = DConnectUtil.getIPAddress(getActivity());
// Set Host IP Address.
EditTextPreference editHostPreferences = (EditTextPreference) getPreferenceScreen()
.findPreference(getString(R.string.key_settings_dconn_host));
editHostPreferences.setSummary(ipAddress);
// Set Host IP Address.
EditTextPreference webHostPref = (EditTextPreference)
getPreferenceScreen().findPreference(getString(R.string.key_settings_web_server_host));
webHostPref.setSummary(ipAddress);
}
private void initNetworkSettings() {
((CheckBoxPreference) findPreference(getString(R.string.pref_wifi_only_key))).setChecked(mPrefs.isWifiOnlyEnabled());
// Disable UPnP if ICE si enabled, or disable ICE if UPnP is enabled
CheckBoxPreference ice = (CheckBoxPreference) findPreference(getString(R.string.pref_ice_enable_key));
CheckBoxPreference turn = (CheckBoxPreference) findPreference(getString(R.string.pref_turn_enable_key));
ice.setChecked(mPrefs.isIceEnabled());
turn.setEnabled(mPrefs.getStunServer() != null);
turn.setChecked(mPrefs.isTurnEnabled());
EditTextPreference turnUsername = (EditTextPreference) findPreference(getString(R.string.pref_turn_username_key));
EditTextPreference turnPassword = (EditTextPreference) findPreference(getString(R.string.pref_turn_passwd_key));
turnUsername.setEnabled(mPrefs.isTurnEnabled());
turnUsername.setSummary(mPrefs.getTurnUsername());
turnUsername.setText(mPrefs.getTurnUsername());
turnPassword.setEnabled(mPrefs.isTurnEnabled());
CheckBoxPreference randomPort = (CheckBoxPreference) findPreference(getString(R.string.pref_transport_use_random_ports_key));
randomPort.setChecked(mPrefs.isUsingRandomPort());
// Disable sip port choice if port is random
EditTextPreference sipPort = (EditTextPreference) findPreference(getString(R.string.pref_sip_port_key));
sipPort.setEnabled(!randomPort.isChecked());
sipPort.setSummary(mPrefs.getSipPort());
sipPort.setText(mPrefs.getSipPort());
EditTextPreference stun = (EditTextPreference) findPreference(getString(R.string.pref_stun_server_key));
stun.setSummary(mPrefs.getStunServer());
stun.setText(mPrefs.getStunServer());
((CheckBoxPreference) findPreference(getString(R.string.pref_push_notification_key))).setChecked(mPrefs.isPushNotificationEnabled());
((CheckBoxPreference) findPreference(getString(R.string.pref_ipv6_key))).setChecked(mPrefs.isUsingIpv6());
}
/**
* Добавить в группу параметров (на экран/в категорию) новую категорию настроек прокси-сервера
* @param group группа, на которую добавляются параметры
*/
protected void addProxyPreferences(PreferenceGroup group) {
final Context context = group.getContext();
PreferenceCategory proxyCat = new PreferenceCategory(context); //категория настроек прокси
proxyCat.setTitle(R.string.pref_cat_proxy);
group.addPreference(proxyCat);
CheckBoxPreference useProxyPref = new LazyPreferences.CheckBoxPreference(context); //чекбокс "использовать ли прокси вообще"
useProxyPref.setTitle(R.string.pref_use_proxy);
useProxyPref.setSummary(R.string.pref_use_proxy_summary);
useProxyPref.setKey(getSharedKey(PREF_KEY_USE_PROXY));
useProxyPref.setDefaultValue(false);
useProxyPref.setOnPreferenceChangeListener(updateHttpListener);
proxyCat.addPreference(useProxyPref);
EditTextPreference proxyHostPref = new LazyPreferences.EditTextPreference(context); //поле ввода адреса прокси-сервера
proxyHostPref.setTitle(R.string.pref_proxy_host);
proxyHostPref.setDialogTitle(R.string.pref_proxy_host);
proxyHostPref.setSummary(R.string.pref_proxy_host_summary);
proxyHostPref.setKey(getSharedKey(PREF_KEY_PROXY_HOST));
proxyHostPref.setDefaultValue(DEFAULT_PROXY_HOST);
proxyHostPref.getEditText().setSingleLine();
proxyHostPref.getEditText().setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI);
proxyHostPref.setOnPreferenceChangeListener(updateHttpListener);
proxyCat.addPreference(proxyHostPref);
proxyHostPref.setDependency(getSharedKey(PREF_KEY_USE_PROXY));
EditTextPreference proxyHostPort = new LazyPreferences.EditTextPreference(context); //поле ввода порта прокси-сервера
proxyHostPort.setTitle(R.string.pref_proxy_port);
proxyHostPort.setDialogTitle(R.string.pref_proxy_port);
proxyHostPort.setSummary(R.string.pref_proxy_port_summary);
proxyHostPort.setKey(getSharedKey(PREF_KEY_PROXY_PORT));
proxyHostPort.setDefaultValue(DEFAULT_PROXY_PORT);
proxyHostPort.getEditText().setSingleLine();
proxyHostPort.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
proxyHostPort.setOnPreferenceChangeListener(updateHttpListener);
proxyCat.addPreference(proxyHostPort);
proxyHostPort.setDependency(getSharedKey(PREF_KEY_USE_PROXY));
}
private void updateDefaultBoardSummary() {
EditTextPreference preference = defaultBoardPreference;
String text = preference.getText();
if (!StringUtils.isEmpty(text)) {
String boardName = StringUtils.validateBoardName(text);
if (boardName != null) {
text = StringUtils.formatBoardTitle(chanName, boardName,
ChanConfiguration.get(chanName).getBoardTitle(boardName));
} else {
text = null;
}
}
preference.setSummary(text);
}
protected void disableDependentOnPairHitAlarm() {
EditTextPreference[] edits = { mUpperLimitPref, mLowerLimitPref };
for (EditTextPreference edit : edits) {
edit.setEnabled(false);
edit.setSummary(null);
}
}
/**
* EditTextPreference の summary に値を表示するように設定します.
*
* @param key EditTextPreference のキー
*/
private void setEditTextPreferenceSummary(String key) {
EditTextPreference pref = (EditTextPreference) getPreferenceScreen().findPreference(key);
pref.setSummary(pref.getText());
pref.setOnPreferenceChangeListener(this);
}
private void updatePreference(Preference preference) {
if (preference != null && preference instanceof EditTextPreference) {
EditTextPreference editTextPreference = (EditTextPreference) preference;
editTextPreference.setSummary(editTextPreference.getText());
}
}
private void setUpPreferences() {
setCurrentValue("ui.theme");
setCurrentValue("theme");
displayLimitPreference = (EditTextPreference) findPreference(getString(R.string.pref_display_limit));
int displayLimitValue = PreferenceHelper.getDisplayLimitPreference(getActivity());
displayLimitPreference.setSummary(getString(R.string.pref_display_limit_summary,
displayLimitValue, getString(R.string.pref_display_limit_default)));
displayLimitPreference.setOnPreferenceChangeListener(this);
filterPatternPreference = (EditTextPreference) findPreference(getString(R.string.pref_filter_pattern));
filterPatternPreference.setSummary(getString(R.string.pref_filter_pattern_summary));
filterPatternPreference.setOnPreferenceChangeListener(this);
logLinePeriodPreference = (EditTextPreference) findPreference(getString(R.string.pref_log_line_period));
int logLinePrefValue = PreferenceHelper.getLogLinePeriodPreference(getActivity());
logLinePeriodPreference.setSummary(getString(R.string.pref_log_line_period_summary,
logLinePrefValue, getString(R.string.pref_log_line_period_default)));
logLinePeriodPreference.setOnPreferenceChangeListener(this);
textSizePreference = (ListPreference) findPreference(getString(R.string.pref_text_size));
textSizePreference.setSummary(textSizePreference.getEntry());
textSizePreference.setOnPreferenceChangeListener(this);
defaultLevelPreference = (ListPreference) findPreference(getString(R.string.pref_default_log_level));
defaultLevelPreference.setOnPreferenceChangeListener(this);
setDefaultLevelPreferenceSummary(defaultLevelPreference.getEntry());
mThemePreference = findPreference("ui.theme");
mThemePreference.setOnPreferenceChangeListener(this);
bufferPreference = (MultipleChoicePreference) findPreference(getString(R.string.pref_buffer));
bufferPreference.setOnPreferenceChangeListener(this);
setBufferPreferenceSummary(bufferPreference.getValue());
mThemePreference.setOnPreferenceChangeListener(this);
mAboutPreference = findPreference(getString(R.string.pref_about));
mAboutPreference.setOnPreferenceClickListener(preference -> {
// launch about activity
Intent intent = new Intent(getActivity(), AboutDialogActivity.class);
startActivity(intent);
return true;
});
mAboutPreference.setSummary(getString(R.string.version, PackageHelper.getVersionName(getActivity())));
scrubberPreference = (SwitchPreference) getPreferenceScreen().findPreference("scrubber");
scrubberPreference.setOnPreferenceChangeListener((preference, newValue) -> {
LogLine.isScrubberEnabled = (boolean) newValue;
return true;
});
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = SimplicityApplication.getContextOfApplication();
mAuth = FirebaseAuth.getInstance();
preferences = PreferenceManager.getDefaultSharedPreferences(context);
addPreferencesFromResource(R.xml.settings);
sync = findPreference("sync_click");
EditTextPreference pref = (EditTextPreference) findPreference("homepage");
pref.setSummary(UserPreferences.getString("homepage",""));
myPrefListner = (prefs, key) -> {
UserPreferences.putString("should_sync", "true");
switch (key) {
case "homepage":
EditTextPreference pref1 = (EditTextPreference) findPreference("homepage");
if (pref1.getText().contains("http://") || pref1.getText().contains("https://")) {
pref1.setSummary(pref1.getText());
preferences.edit().putString("homepage", pref1.getText()).apply();
} else if (!pref1.getText().contains("http://") || !pref1.getText().contains("https://")) {
pref1.setSummary("http://" + pref1.getText());
preferences.edit().putString("homepage", "http://" + pref1.getText()).apply();
}
break;
case "enable_location":
if (preferences.getBoolean("enable_location", false)) {
requestLocationPermission();
} else {
Log.i("Settings", "Location enabled");
}
break;
case "address_bar":
preferences.edit().putString("needs_change", "true").apply();
break;
case "dark_mode":
SettingsActivity.showSheet();
break;
default:
break;
}
};
Preference overflow = findPreference("about_app");
Preference terms = findPreference("terms_set");
Preference donate = findPreference("sim_donate");
Preference backup_restore = findPreference("back_restore");
Preference policy = findPreference("privacy_policy_set");
Preference plugins = findPreference("plugins");
overflow.setOnPreferenceClickListener(this);
donate.setOnPreferenceClickListener(this);
terms.setOnPreferenceClickListener(this);
policy.setOnPreferenceClickListener(this);
backup_restore.setOnPreferenceClickListener(this);
plugins.setOnPreferenceClickListener(this);
sync.setOnPreferenceClickListener(this);
cache = findPreference("delete_cache");
initializeCache();
cache.setOnPreferenceClickListener(this);
}