gpt4 book ai didi

android - 在首选项屏幕 Android 上显示摘要

转载 作者:行者123 更新时间:2023-11-29 00:41:57 25 4
gpt4 key购买 nike

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

这是一个代码 fragment :

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

最佳答案

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

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

为了访问首选项屏幕中的嵌入式 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());

像魅力一样工作!

关于android - 在首选项屏幕 Android 上显示摘要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8805381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com