在首选屏幕Android上显示摘要

时间:2012-01-10 15:03:07

标签: android settings sharedpreferences

我有一个PreferenceScreen,里面有一个PreferenceScreen。我能够在启动时在我最外面的PreferenceScreen中显示摘要,并且还更新了onSharedPreferenceChanged的摘要,但是我的内部PreferenceScreen(在xml中它有字符串@ string / advanced)没有得到初始值...它确实更新onSharedPreferenceChanged。我还需要初始显示。这是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="main_pref">
    <PreferenceCategory
        android:title="@string/location_and_notifications">
        <ListPreference
            android:key="pref_temp_notifications"
            android:title="@string/notifications"
            android:entries="@array/pref_temp_notifications"
            android:entryValues="@array/pref_temp_notifications_values"
            android:dialogTitle="@string/notifications"
            android:defaultValue="2"/>
        <ListPreference
            android:key="pref_notification_interval"
            android:title="@string/notification_interval"
            android:entries="@array/pref_notification_interval"
            android:entryValues="@array/pref_notification_interval_values"
            android:dialogTitle="@string/notification_interval"
            android:defaultValue="15" />
        <ListPreference
            android:key="pref_open_at_launch"
            android:title="@string/open_at_launch"
            android:entries="@array/pref_open_at_launch"
            android:entryValues="@array/pref_open_at_launch_values"
            android:dialogTitle="@string/open_at_launch" 
            android:defaultValue="1"/>
        <ListPreference
            android:key="pref_push_notification"
            android:title="@string/push_enabled"
            android:entries="@array/pref_push_notification"
            android:entryValues="@array/pref_push_notification_values"
            android:dialogTitle="@string/push_enabled"
            android:defaultValue="1"/>
    </PreferenceCategory>
    <PreferenceCategory
            android:title="@string/units">
           <ListPreference
               android:key="pref_temp_units"
               android:title="@string/temperature"
               android:defaultValue="@string/default_metric"
               android:entries="@array/pref_temp_units"
               android:entryValues="@array/pref_temp_units_values"
               android:dialogTitle="@string/units" />
            <PreferenceScreen
                android:title="@string/advanced"
                android:key="advanced_pref">
                <ListPreference
                   android:key="pref_speed"
                   android:title="@string/speed"
                   android:entries="@array/pref_speed"
                   android:entryValues="@array/pref_speed_values"
                   android:defaultValue="@string/default_metric"/>
                <ListPreference
                   android:key="pref_measurement"
                   android:title="@string/measurement"
                   android:entries="@array/pref_measurement"
                   android:entryValues="@array/pref_measurement_values"
                   android:defaultValue="@string/default_metric"/>
                <ListPreference
                   android:key="pref_time"
                   android:title="@string/time_format"
                   android:entries="@array/pref_time"
                   android:entryValues="@array/pref_time_values"
                   android:defaultValue="@string/default_metric"/>
                <ListPreference
                   android:key="pref_date"
                   android:title="@string/date_format"
                   android:entries="@array/pref_date"
                   android:entryValues="@array/pref_date_values"
                   android:defaultValue="@string/default_metric"/>
           </PreferenceScreen>
    </PreferenceCategory>
    <PreferenceCategory
            android:title="@string/personalization">
          <ListPreference
               android:key="pref_color_theme"
               android:title="@string/color_theme"
               android:entries="@array/pref_color_theme"
               android:entryValues="@array/pref_color_theme_values"
               android:dialogTitle="@string/color_theme"
               android:defaultValue="-10981143" />
    </PreferenceCategory>
    <PreferenceCategory
        android:title="@string/more">
        <Preference
            android:title="@string/platinum"
            android:summary="@string/disable_ads"
            android:key="upgradePref"/>
        <Preference
            android:title="@string/rate_update"
            android:summary="@string/rate_app"
            android:key="ratePref"/>
    </PreferenceCategory>
    <PreferenceCategory
          android:title="@string/about">
        <Preference
                android:title="@string/eula"
                android:summary=""
                android:key="eulaPref"/>

        <Preference
                android:title="@string/privacy_policy"
                android:summary=""
                android:key="privacyPolicyPref"/>
         <PreferenceScreen
                android:title="@string/version"
                android:summary=""
                android:key="version">
        </PreferenceScreen>


        <PreferenceScreen
                android:title="@string/customer_support"
                android:summary="@string/email_us">
                <intent android:action="com.accuweather.android.EMAIL_ACCUWX"
                 />
        </PreferenceScreen>
       <PreferenceScreen
                android:title="@string/accuweather_branding"
                android:summary=""
                android:key="accuweatherBranding">
        </PreferenceScreen>      
    </PreferenceCategory>
</PreferenceScreen>

以下是代码段:

在OnCreate中我称之为:

    for(int i=0;i<getPreferenceScreen().getPreferenceCount();i++){
       initSummary(getPreferenceScreen().getPreference(i));
    }

private void initSummary(Preference p) {
   if (p instanceof PreferenceCategory){
     PreferenceCategory pCat = (PreferenceCategory)p;
     for(int i=0;i<pCat.getPreferenceCount();i++){
        initSummary(pCat.getPreference(i));
     }
   }else{
     updatePrefSummary(p);
   }

}

private void updatePrefSummary(Preference p) {
     if (p instanceof ListPreference) {
          ListPreference listPref = (ListPreference) p; 
          p.setSummary(listPref.getEntry()); 
      }
}

然后在onSharedPreferenceChanged:

@Override
public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
    Log.i(DEBUG_TAG, "value of key is " + key);
    Preference pref = findPreference(key);

        ...

     updatePrefSummary(findPreference(key));
}

1 个答案:

答案 0 :(得分:2)

首先,我在另一个类中有这些常量来引用:

public static final String PREF_TIME                = "pref_time";  
public static final String PREF_DATE                = "pref_date";
public static final String PREF_SPEED               = "pref_speed";
public static final String PREF_MEASUREMENT         = "pref_measurement";

为了访问Preference Screen中的嵌入式PreferenceScreen,我执行以下操作:

 // enable summary of one deep preferenceScreen
 Preference advancedSpeedPref = (Preference) findPreference(ACCUWX.Preferences.PREF_SPEED);
 ListPreference listPref1 = (ListPreference) advancedSpeedPref; 
 advancedSpeedPref.setSummary(listPref1.getEntry());

 Preference advancedMeasurementPref = (Preference) findPreference(ACCUWX.Preferences.PREF_MEASUREMENT);
 ListPreference listPref2 = (ListPreference) advancedMeasurementPref; 
 advancedMeasurementPref.setSummary(listPref2.getEntry());

 Preference advancedTimeformatPref = (Preference) findPreference(ACCUWX.Preferences.PREF_TIME);
 ListPreference listPref3 = (ListPreference) advancedTimeformatPref; 
 advancedTimeformatPref.setSummary(listPref3.getEntry());

 Preference advancedDateformatPref = (Preference) findPreference(ACCUWX.Preferences.PREF_DATE);
 ListPreference listPref4 = (ListPreference) advancedDateformatPref; 
 advancedDateformatPref.setSummary(listPref4.getEntry());

像魅力一样!