Android 首选项屏幕上显示摘要
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我在另一个类中有这些常量可供引用:
为了访问首选项屏幕中嵌入的首选项屏幕,我这样做:
工作得像一个魅力!
First, I have these constants in another class to reference:
In order to get access to the embedded PreferenceScreen within the Preference Screen I do this:
Works like a charm!