复位的preference默认值 [英] Reset default values of Preference

查看:179
本文介绍了复位的preference默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的CheckBox preference的设置画面。 XML是:

I am using CheckBoxPreference for settings screen. The XML is:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen  xmlns:android="http://schemas.android.com/apk/res/android" >
    <CheckBoxPreference android:key="includeAddress"
                    android:title="Include Address" 
                    android:summary=""
                    android:defaultValue="true" />
    <CheckBoxPreference android:key="rememberName"
                    android:title="Remeber Name" 
                    android:summary=""
                    android:defaultValue="false" />
</PreferenceScreen>

我改变值,而在应用程序中。一旦用户注销时,它必须被设置成在XML定义为默认值。但是,这似乎并没有工作。他们让这些价值观我最后的选择。

I change the values while in the application. Once the user logs out, it must be set to default values as defined in the xml. But, it does not seem to work. They keep those values I chose last.

看了Android的文档,我发现这一点:

Having read Android docs, I found this:

PreferenceManager.setDefaultValues(getApplicationContext(), R.xml.preference_settings, true);

但它很难做的工作!试过其他方式轮与共享preferences。

But it hardly does the job! Tried other way round with SharedPreferences.

SharedPreferences preferences = getParent().getSharedPreferences("preference_settings", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();

这也不能工作!

It didn't work either!

我缺少的东西?我怎么可以设置preferences在XML中定义的默认值吗?

Am I missing something? How could I set preferences to their default values defined in the XML?

在此先感谢!

推荐答案

共享preferences应该工作,但你应该使用默认共享preferences ..

the shared preferences should work, but you should use the default shared preferences..

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();

要获取使用的文件名的共享preferences,Android的创建这个名称(可能基于项目的包叫什么名字?)。您可以通过将以下code在SettingsActivity的onCreate,并看到什么preferencesName是得到它。

To get the shared preferences using file name, Android creates this name (possibly based on the package name of your project?). You can get it by putting the following code in a SettingsActivity onCreate, and seeing what preferencesName is.

String preferencesName = this.getPreferenceManager().getSharedPreferencesName();

该字符串应该是这样的com.example.projectname_ preferences。硬code,它放在你的项目,并把它传递到getShared preferences(),你应该是好去。

The string should be something like "com.example.projectname_preferences". Hard code that somewhere in your project, and pass it in to getSharedPreferences() and you should be good to go.

AS:

 PreferenceManager.getDefaultSharedPreferences(this);

将提供一个获得了preferences文件,它是全球整个应用程序包;任何活动都可以访问preferences(internaly,xml文件抱着preferences将被命名为 your.application.package_ preferences.xml )。

getParent().getSharedPreferences("preference_settings", MODE_PRIVATE);

将只提供了contextInstance类preferences:上下文的类可以访问这些preferences(说你的包仍然是唯一的实例 your.application.package 和你在 your.application.package.SecondActivity ,internaly的​​preferences文件是 SecondActivity.xml )。

Will provide preferences only for the contextInstance class: only instances of the context's class can access these preferences (said your package is still your.application.package and you're in your.application.package.SecondActivity, internaly the preferences file is SecondActivity.xml).

这篇关于复位的preference默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆