我的 PreferenceActivity 中的对话框首选项未保存
我有一堆 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在所有控件(EditTextPreference 和 TimePickerPreference)中定义
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.
我遇到了同样的问题,PreferenceActivity 似乎可以正确读取首选项,但在用户更改某些属性后,我没有看到 myPreferences 中的更改。在 PreferencesActivity 更新首选项后,在访问首选项之前,您所需要做的就是重新创建 myPreferences,如下所示:
... 看来,首选项的两个实例未同步(可能是因为它以某种方式缓存)。一个实例中的更改不会出现在另一个实例中,尽管它们具有相同的名称(或者它们都是默认共享首选项)。
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:
... 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).