下面列出了android.preference.ListPreference#setKey ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void addRatingPreference(PreferenceGroup group) {
Context context = group.getContext();
Preference.OnPreferenceChangeListener updateRatingListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference.getKey().equals(getSharedKey(PREF_KEY_MAX_RATING))) {
setMaxRating((String) newValue);
return true;
}
return false;
}
};
ListPreference ratingPref = new LazyPreferences.ListPreference(context);
ratingPref.setTitle(R.string.dobrochan_prefs_max_rating);
ratingPref.setSummary(preferences.getString(getSharedKey(PREF_KEY_MAX_RATING), "R-15"));
ratingPref.setEntries(RATINGS);
ratingPref.setEntryValues(RATINGS);
ratingPref.setDefaultValue("R-15");
ratingPref.setKey(getSharedKey(PREF_KEY_MAX_RATING));
ratingPref.setOnPreferenceChangeListener(updateRatingListener);
group.addPreference(ratingPref);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Make sure default values are applied. In a real app, you would
// want this in a shared function that is used to retrieve the
// SharedPreferences wherever they are needed.
// PreferenceManager.setDefaultValues(getActivity(),
// R.xml.advanced_preferences, false);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.general_prefs);
ListPreference listPref = new ListPreference(getActivity());
listPref.setKey("fb_app");
listPref.setOrder(0);
NameList apps = IntentUtil.getApps(getActivity());
listPref.setEntries(apps.namesAvail.toArray(new CharSequence[apps.namesAvail.size()]));
listPref.setEntryValues(apps.pkgsAvail.toArray(new String[apps.pkgsAvail.size()]));
listPref.setTitle(getActivity().getString(R.string.fb_app));
listPref.setSummary(getActivity().getString(R.string.fb_app_description));
getPreferenceScreen().addPreference(listPref);
}
private void addBatLevel (final String key, final CharSequence title, final CharSequence defVal, final OnPreferenceChangeListener changeListener) {
final ListPreference pref = new ListPreference(getActivity());
pref.setKey(key);
pref.setTitle(title);
pref.setEntries(CollectionHelper.map(BATTERY_LEVELS, new Function<CharSequence, CharSequence>() {
@Override
public CharSequence exec (final CharSequence input) {
return input + "%";
}
}, new ArrayList<CharSequence>()).toArray(new CharSequence[BATTERY_LEVELS.length]));
pref.setEntryValues(BATTERY_LEVELS);
pref.setSummary("%s");
pref.setDefaultValue(defVal);
pref.setOnPreferenceChangeListener(changeListener);
getPreferenceScreen().addPreference(pref);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPm = getPreferenceManager();
mPs = mPm.createPreferenceScreen(this);
setTitle(R.string.label_customize_shortcut);
int len = TBL_SUMMARY.length;
String[] tbl_summary = new String[ len ];
String[] tbl_function = new String[ len ];
for( int i=0;i<len;i++ ){
tbl_summary[i] = getString(TBL_SUMMARY[i]);
tbl_function[i] = Integer.toString(TBL_FUNCTION[i]);
}
for( DefineShortcut sd : TBL_SHORTCUT )
{
final ListPreference pr = new ListPreference(this);
pr.setKey(KEY_SHORTCUT + sd.key );
pr.setTitle( sd.name );
pr.setEntries(tbl_summary);
pr.setEntryValues(tbl_function);
mPs.addPreference(pr);
}
setPreferenceScreen(mPs);
setSummary();
}
@Override
public void fillLayout(final SipProfile account) {
super.fillLayout(account);
CharSequence[] states = new CharSequence[] {"act", "nsw", "nt", "qld", "sa", "tas", "vic", "wa"};
accountState = new ListPreference(parent);
accountState.setEntries(states);
accountState.setEntryValues(states);
accountState.setKey("state");
accountState.setDialogTitle(R.string.w_iinet_state);
accountState.setTitle(R.string.w_iinet_state);
accountState.setSummary(R.string.w_iinet_state_desc);
accountState.setDefaultValue("act");
addPreference(accountState);
String domain = account.reg_uri;
if( domain != null ) {
for(CharSequence state : states) {
String currentComp = "sip:sip."+state+".iinet.net.au";
if( currentComp.equalsIgnoreCase(domain) ) {
accountState.setValue(state.toString());
break;
}
}
}
accountUsername.setTitle(R.string.w_iinet_username);
accountUsername.setDialogTitle(R.string.w_iinet_username);
accountPassword.setTitle(R.string.w_iinet_password);
accountPassword.setDialogTitle(R.string.w_iinet_password);
}
@Override
public void addPreferencesOnScreen(PreferenceGroup preferenceGroup) {
Context context = preferenceGroup.getContext();
ListPreference domainPref = new LazyPreferences.ListPreference(context);
domainPref.setKey(getSharedKey(PREF_KEY_DOMAIN));
domainPref.setTitle(R.string.pref_domain);
domainPref.setSummary(resources.getString(R.string.pref_domain_summary, DOMAINS_HINT));
domainPref.setDialogTitle(R.string.pref_domain);
domainPref.setEntries(DOMAINS);
domainPref.setEntryValues(DOMAINS);
domainPref.setDefaultValue(DOMAINS[0]);
preferenceGroup.addPreference(domainPref);
super.addPreferencesOnScreen(preferenceGroup);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPm = getPreferenceManager();
mPs = mPm.createPreferenceScreen(this);
setTitle(R.string.label_customize_shortcut);
int len = TBL_SUMMARY.length;
String[] tbl_summary = new String[ len ];
String[] tbl_function = new String[ len ];
for( int i=0;i<len;i++ ){
tbl_summary[i] = getString(TBL_SUMMARY[i]);
tbl_function[i] = Integer.toString(TBL_FUNCTION[i]);
}
for( DefineShortcut sd : TBL_SHORTCUT )
{
final ListPreference pr = new ListPreference(this);
pr.setKey(KEY_SHORTCUT + sd.key );
pr.setTitle( sd.name );
pr.setEntries(tbl_summary);
pr.setEntryValues(tbl_function);
mPs.addPreference(pr);
}
setPreferenceScreen(mPs);
setSummary();
}
public ListPreference makeList(PreferenceGroup parent, String key, CharSequence[] values,
String defaultValue, int titleResId, CharSequence[] entries) {
ListPreference preference = new ListPreference(getActivity());
preference.setKey(key);
preference.setTitle(titleResId);
preference.setDialogTitle(titleResId);
preference.setEntries(entries);
preference.setEntryValues(values);
if (defaultValue != null) {
preference.setDefaultValue(defaultValue);
}
addPreference(parent, preference);
updateListSummary(preference);
return preference;
}
private void addLocalePref () {
final ListPreference pref = new ComboPreference(getActivity());
pref.setKey(KEY_LOCALE);
pref.setTitle("Locale"); //ES
pref.setEntries(SupportedLocales.prefEntries());
pref.setEntryValues(SupportedLocales.prefEntryValues());
pref.setDefaultValue(SupportedLocales.DEFAULT.getValue());
pref.setOnPreferenceChangeListener(this.onLocaleChangeListener);
getPreferenceScreen().addPreference(pref);
}
private void addColumnsCount (final String key, final String title) {
final ListPreference pref = new ComboPreference(getActivity());
pref.setKey(key);
pref.setTitle(title);
pref.setEntries(COUNTS);
pref.setEntryValues(COUNTS);
pref.setDefaultValue(DEFAULT_COUNT);
getPreferenceScreen().addPreference(pref);
}
private void addPrefetchMedia () {
final ListPreference pref = new ListPreference(getActivity());
pref.setKey(KEY_PREFETCH_MEDIA);
pref.setTitle("Prefetch media"); //ES
pref.setSummary("Fetch new pictures during background updates: %s"); //ES
pref.setEntries(PrefetchMode.prefEntries());
pref.setEntryValues(PrefetchMode.prefEntryValues());
pref.setDefaultValue(PrefetchMode.NO.getValue());
getPreferenceScreen().addPreference(pref);
}
private void addPrefetchLinks () {
final ListPreference pref = new ListPreference(getActivity());
pref.setKey(KEY_PREFETCH_LINKS);
pref.setTitle("Prefetch links"); //ES
pref.setSummary("Fetch new link titles during background updates: %s"); //ES
pref.setEntries(PrefetchMode.prefEntries());
pref.setEntryValues(PrefetchMode.prefEntryValues());
pref.setDefaultValue(PrefetchMode.NO.getValue());
getPreferenceScreen().addPreference(pref);
}
public static ListPreference List(Context context, PreferenceCategory category, String caption, String summary, String dialogCaption, String key, Object defaultValue, CharSequence[] entries, CharSequence[] entryValues, boolean enabled) {
ListPreference retval = new ListPreference(context);
retval.setTitle(caption);
retval.setSummary(summary);
retval.setEnabled(enabled);
retval.setKey(key);
retval.setDefaultValue(defaultValue);
retval.setDialogTitle(dialogCaption);
retval.setEntries(entries);
retval.setEntryValues(entryValues);
if (category != null) category.addPreference(retval);
return retval;
}
public static ListPreference List(Context context, PreferenceCategory category, int caption, int summary, int dialogCaption, String key, Object defaultValue, CharSequence[] entries, CharSequence[] entryValues, boolean enabled) {
ListPreference retval = new ListPreference(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);
retval.setEntries(entries);
retval.setEntryValues(entryValues);
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;
}