我的 PreferenceActivity 中的对话框首选项未保存

发布于 2025-01-04 16:00:19 字数 2075 浏览 0 评论 0原文

我有一堆 EditTextPreferences 和一个 TimePickerPreference(我创建的)以及一个 CheckBoxPreference,并且没有保存任何 EditTextPreference 或 TimePickerPreference,但 CheckBox 保存了。我认为这可能是因为我的 PreferenceActivity 子类中的最低限度,但看到复选框如何保存,我不确定是否是这样。

我搜遍了所有内容,似乎都说首选项应该自动保存,但我找不到任何不这样做的原因。

这是我的偏好 xml 文件:

<PreferenceCategory android:title="@string/settings_general_header" >
    <EditTextPreference
        android:defaultValue="15"
        android:summary="@string/settings_time_to_sleep"
        android:title="@string/settings_time_to_sleep_title" 
        android:numeric="integer" />
    <EditTextPreference
        android:defaultValue="90"
        android:summary="@string/settings_sleep_cycle_time_summary"
        android:title="@string/settings_sleep_cycle_minutes" 
        android:numeric="integer" />
    <EditTextPreference
        android:defaultValue="5"
        android:summary="@string/how_many_results"
        android:title="@string/settings_results_to_show_title" 
        android:numeric="integer" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/wakeupat_settings_header" >
    <CheckBoxPreference
        android:key="@string/save_wakeupat_time"
        android:title="@string/save_wakeupat_time" android:defaultValue="false"/>

    <TimePickerPreference
        android:dependency="@string/save_wakeupat_time"
        android:title="@string/wakeupat_preferred_time" />
</PreferenceCategory>

这是我的 PreferenceActivity 子类:

package com.krej.timeforbed;

import java.util.List;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Button;

public class Settings extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.settings);
    }

}

I have a bunch of EditTextPreferences and a TimePickerPrefernce(that I created) along with one CheckBoxPreference and none of the EditTextPreference or TimePickerPreference get saved, but the CheckBox does. I thought it might have been because of the bare minimum I have in my PreferenceActivity subclass, but seeing as how the checkbox gets saved I'm not sure if that is.

I've searched all over and everything seems to say that preferences should be automatically saved, and I can't find out any reason why they wouldn't be.

Here is my preference xml file:

<PreferenceCategory android:title="@string/settings_general_header" >
    <EditTextPreference
        android:defaultValue="15"
        android:summary="@string/settings_time_to_sleep"
        android:title="@string/settings_time_to_sleep_title" 
        android:numeric="integer" />
    <EditTextPreference
        android:defaultValue="90"
        android:summary="@string/settings_sleep_cycle_time_summary"
        android:title="@string/settings_sleep_cycle_minutes" 
        android:numeric="integer" />
    <EditTextPreference
        android:defaultValue="5"
        android:summary="@string/how_many_results"
        android:title="@string/settings_results_to_show_title" 
        android:numeric="integer" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/wakeupat_settings_header" >
    <CheckBoxPreference
        android:key="@string/save_wakeupat_time"
        android:title="@string/save_wakeupat_time" android:defaultValue="false"/>

    <TimePickerPreference
        android:dependency="@string/save_wakeupat_time"
        android:title="@string/wakeupat_preferred_time" />
</PreferenceCategory>

and here is my PreferenceActivity subclass:

package com.krej.timeforbed;

import java.util.List;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Button;

public class Settings extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.settings);
    }

}

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

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

发布评论

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

评论(2

海之角 2025-01-11 16:00:20

您需要在所有控件(EditTextPreferenceTimePickerPreference)中定义 android:key 以使框架自动保存值。

由于您已经在 CheckBoxPreference 中定义了它,这就是它自动保存它的原因。

you need to define android:key in all your controls (EditTextPreference and TimePickerPreference) to let the framework save values automatically.

And since you have defined it in CheckBoxPreference that's why its saving it automatically.

木森分化 2025-01-11 16:00:20

我遇到了同样的问题,PreferenceActivity 似乎可以正确读取首选项,但在用户更改某些属性后,我没有看到 myPreferences 中的更改。在 PreferencesActivity 更新首选项后,在访问首选项之前,您所需要做的就是重新创建 myPreferences,如下所示:

myPreferences=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

... 看来,首选项的两个实例未同步(可能是因为它以某种方式缓存)。一个实例中的更改不会出现在另一个实例中,尽​​管它们具有相同的名称(或者它们都是默认共享首选项)。

I had the same problem, that PreferenceActivity seemed to read preferences correcly, but I didn't see changes in myPreferences after user had changed some attributes. All you need before you access preferences after they are updated by PreferencesActivity is to recreate myPreferences like this:

myPreferences=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

... it seems, that two instances of preferences are not synchronized (may be because it is cached somehow). Changes in one instance doesn't appear in another although they are both of the same name (or they are both default shared prefereneces).

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