Android 首选项屏幕上显示摘要

发布于 2024-12-25 20:12:29 字数 6963 浏览 1 评论 0原文

我有一个 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));
}

I have a PreferenceScreen that has a PreferenceScreen within it. I am able to display a summary in my outer most PreferenceScreen on launch and also update the summaries onSharedPreferenceChanged, but my inner PreferenceScreen (in xml it has string @string/advanced) does not get the initial value...it does update onSharedPreferenceChanged. I need the intial to display as well. Here is my 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>

Here is a code snippet:

in OnCreate I call this:

    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()); 
      }
}

Then in 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));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌伤浅笑 2025-01-01 20:12:29

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

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";

为了访问首选项屏幕中嵌入的首选项屏幕,我这样做:

 // 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());

工作得像一个魅力!

First, I have these constants in another class to reference:

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";

In order to get access to the embedded PreferenceScreen within the Preference Screen I do this:

 // 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());

Works like a charm!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文