下面列出了android.preference.EditTextPreference#setKey ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
/**
* Добавить в группу параметров (на экран/в категорию) параметр задания пароля для удаления постов/файлов
* @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 addDomainPreferences(PreferenceGroup group) {
Context context = group.getContext();
Preference.OnPreferenceChangeListener updateDomainListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference.getKey().equals(getSharedKey(PREF_KEY_DOMAIN))) {
domain = (String) newValue;
if (domain.length() == 0) domain = DEFAULT_DOMAIN;
loadHanabiraCookie();
return true;
}
return false;
}
};
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);
domainPref.setOnPreferenceChangeListener(updateDomainListener);
group.addPreference(domainPref);
}
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 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);
}
private PreferenceScreen createFromProvider() {
PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
if (provider.credentialTypeToParameterNames().isEmpty()) {
return root;
}
String credentialType = provider.credentialTypeToParameterNames().keySet().iterator().next();
root.setTitle(credentialType + " credentials for provider " + provider.name());
for (String parameter : provider.credentialTypeToParameterNames().get(credentialType)) {
EditTextPreference cred = new EditTextPreference(this);
cred.setKey(parameter);
cred.setTitle(parameter);
cred.setDialogTitle(parameter);
root.addPreference(cred);
}
return root;
}
/**
* Добавить в группу параметров (на экран/в категорию) новую категорию настроек прокси-сервера
* @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));
}
@Override
public void addPreferencesOnScreen(PreferenceGroup preferenceGroup) {
final Context context = preferenceGroup.getContext();
EditTextPreference passwordPref = new EditTextPreference(context);
passwordPref.setTitle(R.string.iichan_prefs_report_thread);
passwordPref.setDialogTitle(R.string.iichan_prefs_report_thread);
passwordPref.setSummary(R.string.iichan_prefs_report_thread_summary);
passwordPref.setKey(getSharedKey(PREF_KEY_REPORT_THREAD));
passwordPref.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
passwordPref.getEditText().setSingleLine();
passwordPref.getEditText().setFilters(new InputFilter[] { new InputFilter.LengthFilter(255) });
preferenceGroup.addPreference(passwordPref);
super.addPreferencesOnScreen(preferenceGroup);
}
private void addDomainPreference(PreferenceGroup group) {
Context context = group.getContext();
EditTextPreference domainPref = new EditTextPreference(context);
domainPref.setTitle(R.string.pref_domain);
domainPref.setSummary(resources.getString(R.string.pref_domain_summary, DOMAINS_HINT));
domainPref.setDialogTitle(R.string.pref_domain);
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);
group.addPreference(domainPref);
}
/** Добавить категорию настроек домена (в т.ч. https) */
private void addDomainPreferences(PreferenceGroup group) {
Context context = group.getContext();
Preference.OnPreferenceChangeListener updateDomainListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference.getKey().equals(getSharedKey(PREF_KEY_DOMAIN))) {
updateDomain((String) newValue, preferences.getBoolean(getSharedKey(PREF_KEY_USE_HTTPS_MAKABA), true));
return true;
} else if (preference.getKey().equals(getSharedKey(PREF_KEY_USE_HTTPS_MAKABA))) {
updateDomain(preferences.getString(getSharedKey(PREF_KEY_DOMAIN), DEFAULT_DOMAIN), (boolean)newValue);
return true;
}
return false;
}
};
PreferenceCategory domainCat = new PreferenceCategory(context);
domainCat.setTitle(R.string.makaba_prefs_domain_category);
group.addPreference(domainCat);
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);
domainPref.setOnPreferenceChangeListener(updateDomainListener);
domainCat.addPreference(domainPref);
CheckBoxPreference httpsPref = new LazyPreferences.CheckBoxPreference(context); //чекбокс "использовать https"
httpsPref.setTitle(R.string.pref_use_https);
httpsPref.setSummary(R.string.pref_use_https_summary);
httpsPref.setKey(getSharedKey(PREF_KEY_USE_HTTPS_MAKABA));
httpsPref.setDefaultValue(true);
httpsPref.setOnPreferenceChangeListener(updateDomainListener);
domainCat.addPreference(httpsPref);
}
private void addPreferencesForOneAccount(int accountId) {
MyLog.entry("accountId = " + accountId);
final PreferenceCategory accountCategory = new PreferenceCategory(getActivity());
accountCategory.setTitle(getString(R.string.settings_padherder_account_category, accountId));
accountsByPosition.put(accountId, accountCategory);
getPreferenceScreen().addPreference(accountCategory);
final EditTextPreference accountName = new EditTextPreference(getActivity());
accountName.setKey("padherder_name_" + accountId);
accountName.setTitle(R.string.settings_padherder_name_title);
accountName.setDialogTitle(R.string.settings_padherder_name_title);
accountName.setSummary(R.string.settings_padherder_name_summary);
accountCategory.addPreference(accountName);
final EditTextPreference accountLogin = new EditTextPreference(getActivity());
accountLogin.setKey("padherder_login_" + accountId);
accountLogin.setTitle(R.string.settings_padherder_login_title);
accountLogin.setDialogTitle(R.string.settings_padherder_login_title);
accountLogin.setSummary(R.string.settings_padherder_login_summary);
accountCategory.addPreference(accountLogin);
final EditTextPreference accountPassword = new EditTextPreference(getActivity());
accountPassword.setKey("padherder_password_" + accountId);
accountPassword.setTitle(R.string.settings_padherder_password_title);
accountPassword.setDialogTitle(R.string.settings_padherder_password_title);
accountPassword.setSummary(R.string.settings_padherder_password_summary);
accountPassword.getEditText().setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
accountCategory.addPreference(accountPassword);
MyLog.exit();
}
public static EditTextPreference Edit(Context context, PreferenceCategory category, String caption, String summary, String dialogCaption, String key, Object defaultValue, boolean enabled, Integer type) {
EditTextPreference retval = new EditTextPreference(context);
retval.setTitle(caption);
retval.setSummary(summary);
retval.setEnabled(enabled);
retval.setKey(key);
retval.setDefaultValue(defaultValue);
retval.setDialogTitle(dialogCaption);
if (type != null) {
retval.getEditText().setInputType(type);
}
if (category != null) category.addPreference(retval);
return retval;
}
public static EditTextPreference Edit(Context context, PreferenceCategory category, int caption, int summary, int dialogCaption, String key, Object defaultValue, boolean enabled, Integer type) {
EditTextPreference retval = new EditTextPreference(context);
if (caption > 0) retval.setTitle(caption);
if (summary > 0) retval.setSummary(summary);
retval.setEnabled(enabled);
retval.setKey(key);
retval.setDefaultValue(defaultValue);
if (dialogCaption > 0) retval.setDialogTitle(dialogCaption);
if (type != null) {
retval.getEditText().setInputType(type);
}
if (category != null) category.addPreference(retval);
return retval;
}
private PreferenceScreen createPreferenceHierarchy() {
CharSequence[] cs = new String[] { "Terminal Emulator", "Run Activity", "auto" };
CharSequence[] cs2 = new String[] { "off", "on" };
// Root
PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this);
dialogBasedPrefCat.setTitle("install");
root.addPreference(dialogBasedPrefCat); // Adding a category
// List preference under the category
ListPreference listPref = new ListPreference(this);
listPref.setKey("runmode");
listPref.setDefaultValue(cs[2]);
listPref.setEntries(cs);
listPref.setEntryValues(cs);
listPref.setDialogTitle("run install.sh in");
listPref.setTitle("run install.sh in");
listPref.setSummary("run install.sh in");
dialogBasedPrefCat.addPreference(listPref);
ListPreference rootmode = new ListPreference(this);
rootmode.setKey("rootmode");
rootmode.setDefaultValue(cs2[0]);
rootmode.setEntries(cs2);
rootmode.setEntryValues(cs2);
rootmode.setDialogTitle("run install.sh as superuser");
rootmode.setTitle("run install.sh as superuser");
rootmode.setSummary("root required");
dialogBasedPrefCat.addPreference(rootmode);
PreferenceCategory dialogBasedPrefCat2 = new PreferenceCategory(this);
dialogBasedPrefCat2.setTitle("run");
root.addPreference(dialogBasedPrefCat2); // Adding a category
// List preference under the category
CharSequence[] csjar = new String[] { "Terminal Emulator", "Run Activity" };
ListPreference listPref2 = new ListPreference(this);
listPref2.setKey("runmode2");
listPref2.setDefaultValue(csjar[1]);
listPref2.setEntries(csjar);
listPref2.setEntryValues(csjar);
listPref2.setDialogTitle("run jar file in");
listPref2.setTitle("run jar file in");
listPref2.setSummary("run jar file in");
dialogBasedPrefCat2.addPreference(listPref2);
ListPreference rootmode2 = new ListPreference(this);
rootmode2.setKey("rootmode2");
rootmode2.setDefaultValue(cs2[0]);
rootmode2.setEntries(cs2);
rootmode2.setEntryValues(cs2);
rootmode2.setDialogTitle("run jar file as superuser");
rootmode2.setTitle("run jar file as superuser");
rootmode2.setSummary("root required");
dialogBasedPrefCat2.addPreference(rootmode2);
PreferenceCategory dialogBasedPrefCat3 = new PreferenceCategory(this);
dialogBasedPrefCat3.setTitle("path broadcast");
root.addPreference(dialogBasedPrefCat3); // Adding a category
// List preference under the category
CharSequence[] cspath = new String[] { "on", "off", "if jamvm is installed" };
ListPreference listPref3 = new ListPreference(this);
listPref3.setKey("broadcast");
listPref3.setDefaultValue(cspath[2]);
listPref3.setEntries(cspath);
listPref3.setEntryValues(cspath);
listPref3.setDialogTitle("broadcast path to terminal emulator");
listPref3.setTitle("broadcast path to terminal emulator");
listPref3.setSummary("broadcast path to terminal emulator");
dialogBasedPrefCat3.addPreference(listPref3);
EditTextPreference path = new EditTextPreference(this);
path.setKey("broadcastpath");
path.setDefaultValue("/data/data/julianwi.javainstaller");
path.setDialogTitle("path to broadcast");
path.setTitle("path to broadcast");
path.setSummary("path to broadcast");
dialogBasedPrefCat3.addPreference(path);
return root;
}