开发者

Is there a way to display the current setting of a PreferenceActivity?

开发者 https://www.devze.com 2023-03-03 09:06 出处:网络
I feel like I must be missing something, but I just do开发者_运维问答n\'t see what it is... I have a PreferenceActivity with a bunch of various preferences (some are lists, some are just text fields)

I feel like I must be missing something, but I just do开发者_运维问答n't see what it is... I have a PreferenceActivity with a bunch of various preferences (some are lists, some are just text fields) and it all works fine, but unless I explicitly write each item's value to the summary (which is obviously not intended for this purpose) I don't see how (or where) the items display what they are currently set to. When I click on them the various views show up with the correct settings, but that's clearly not the intention.

Do I have to create my own custom List item of some sort that has a field that displays the currently populated value of each element?


Unfortunately the default PreferencesActivity doesn't display the values: what you're doing is really the way to go if you care to have all the preferences displayed at a glance.


If you still want to go down the programming direction then look at this thread: How do I display the current value of an Android Preference in the Preference summary?

Has everything there.


Create another preference field: summary. Update it whenever a preference field is updated, or when displaying the preferences screen. The user will be able to "update" the summary value, but whenever he/she enters preferences, the correct value will be displayed.


For ListPreferences, this is built-in and you can use

android:summary="Actual value: %s"

For EditTextPreferences, you can easily create your own class:

package your.package.preference;

import android.content.Context;
import android.util.AttributeSet;

public class EditTextPreference extends android.preference.EditTextPreference{
        public EditTextPreference(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

        public EditTextPreference(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public EditTextPreference(Context context) {
            super(context);
        }

        @Override
        public CharSequence getSummary() {
            String summary = super.getSummary().toString();
            return String.format(summary, getText());
        }
    }

And use this in your xml:

<your.package.EditTextPreference
                android:key="pref_alpha"
                android:summary="Actual value: %s"
                android:title="Title"
                android:defaultValue="default"
                />
0

精彩评论

暂无评论...
验证码 换一张
取 消