如何显示的是Android preference在preference总结的当前值? [英] How do I display the current value of an Android Preference in the Preference summary?

查看:523
本文介绍了如何显示的是Android preference在preference总结的当前值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这必须拿出非常频繁。

当用户正在编辑preferences在一个Android应用程序,我希望他们能够看到的preference在 preference <当前设置值/ code>摘要。

When the user is editing preferences in an Android app, I'd like them to be able to see the currently set value of the preference in the Preference summary.

例如:如果我有一个preference设置为放弃旧邮件,指定日子需要的信息之后,要被清理的数量。在 preferenceActivity 我希望用户看到:

Example: if I have a Preference setting for "Discard old messages" that specifies the number of days after which messages need to be cleaned up. In the PreferenceActivity I'd like the user to see:

弃旧消息的&LT; - 标题

"Discard old messages" <- title

清理后的 X 日消息的&LT; - <一个href="http://$c$c.google.com/android/reference/android/$p$pference/$p$pference.html#setSummary(java.lang.CharSequence)">summary其中 X 是当前preference值

"Clean up messages after x days" <- summary where x is the current Preference value

附加题:让这个可重复使用,这样我就可以不管它很容易地应用它们的类型我所有的preferences(这样它的工作与EditText上preference,列表preference等与少量编码)。

Extra credit: make this reusable, so I can easily apply it to all my preferences regardless of their type (so that it work with EditTextPreference, ListPreference etc. with minimal amount of coding).

推荐答案

有办法让这个更通用的解决方案,如果适合您的需求。

There are ways to make this a more generic solution, if that suits your needs.

例如,如果你想一般都列表preferences展示自己的选择,总之,你可以有这种为你的 onShared preferenceChanged 的实施:

For example, if you want to generically have all list preferences show their choice as summary, you could have this for your onSharedPreferenceChanged implementation:

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    Preference pref = findPreference(key);

    if (pref instanceof ListPreference) {
        ListPreference listPref = (ListPreference) pref;
        pref.setSummary(listPref.getEntry());
    }
}

这是很容易扩展到其他preference类。

This is easily extensible to other preference classes.

和使用获取preferenceCount 获取preference 在<$ C $功能C> preferenceScreen preferenceCategory ,你可以很容易地编写一个通用函数来走preference树设置摘要你希望自己的的toString类型的所有preferences的重presentation

And by using the getPreferenceCount and getPreference functionality in PreferenceScreen and PreferenceCategory, you could easily write a generic function to walk the preference tree setting the summaries of all preferences of the types you desire to their toString representation

这篇关于如何显示的是Android preference在preference总结的当前值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆