CheckBoxPreference不会保留选中的值吗?

时间:2014-02-03 17:14:52

标签: android

我有这个偏好

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<CheckBoxPreference
    android:defaultValue="true"
    android:key="sound"
    android:summary="Sound ON/OFF"
    android:title="Sound" >
</CheckBoxPreference>

<PreferenceScreen
    android:key="videos"
    android:persistent="false"
    android:summary="Click to select videos"
    android:title="Videos" >
    <CheckBoxPreference
        android:defaultValue="true"
        android:icon="@drawable/ic_launcher_crimson"
        android:key="crimson"
        android:title="Cimson Cartoon" />
    <CheckBoxPreference
        android:defaultValue="false"
        android:icon="@drawable/ic_launcher_darkgreen"
        android:key="darkgreen"
        android:title="Dark Green Cartoon" />
    <CheckBoxPreference
        android:defaultValue="false"
        android:icon="@drawable/ic_launcher_openfieldrifle"
        android:key="openfieldrifle"
        android:title="Open Field with Bullet Bill, Assault Rifle" />
    <CheckBoxPreference
        android:defaultValue="false"
        android:icon="@drawable/ic_launcher_openfieldshotgun"
        android:key="openfiledshotgun"
        android:title="Open Field Cartoon Shotgun" />
    <CheckBoxPreference
        android:defaultValue="false"
        android:icon="@drawable/ic_launcher_submachine"
        android:key="submachine"
        android:title="Submachine Dark Mist" />
    <CheckBoxPreference
        android:defaultValue="false"
        android:icon="@drawable/ic_launcher_violet"
        android:key="violet"
        android:title="Violet Cartoon" />
</PreferenceScreen>

</PreferenceScreen>

现在,

在我的PreferenceActivity中,显然检查的值不会保留:

public class MyPreferencesActivity extends PreferenceActivity {
Boolean crimson;
Boolean darkgreen;
Boolean openfieldrifle;
Boolean openfiledshotgun;
Boolean submachine;
Boolean violet;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getPreferenceManager().setSharedPreferencesName("prefs");
    addPreferencesFromResource(R.xml.prefs);

    PreferenceManager.setDefaultValues(this, R.xml.prefs, false);
    Preference crimsonpreference = getPreferenceScreen().findPreference(
            "crimson");
    Preference darkgreenpreference = getPreferenceScreen().findPreference(
            "darkgreen");
    Preference openfieldriflepreference = getPreferenceScreen()
            .findPreference("openfieldrifle");
    Preference openfiledshotgunpreference = getPreferenceScreen()
            .findPreference("openfiledshotgun");
    Preference submachinepreference = getPreferenceScreen().findPreference(
            "submachine");
    Preference violetpreference = getPreferenceScreen().findPreference(
            "violet");

    SharedPreferences sharedPref = PreferenceManager
            .getDefaultSharedPreferences(MyPreferencesActivity.this);
    crimson = sharedPref.getBoolean("crimson", false);
    darkgreen = sharedPref.getBoolean("darkgreen", false);
    openfieldrifle = sharedPref.getBoolean("openfieldrifle", false);
    openfiledshotgun = sharedPref.getBoolean("openfiledshotgun", false);
    submachine = sharedPref.getBoolean("submachine", false);
    violet = sharedPref.getBoolean("violet", false);

    crimsonpreference.setOnPreferenceChangeListener(listener1);

}

Preference.OnPreferenceChangeListener listener1 = new OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        SharedPreferences sharedPref = PreferenceManager
                .getDefaultSharedPreferences(MyPreferencesActivity.this);
        crimson = sharedPref.getBoolean("crimson", false);
        darkgreen = sharedPref.getBoolean("darkgreen", false);
        openfieldrifle = sharedPref.getBoolean("openfieldrifle", false);
        openfiledshotgun = sharedPref.getBoolean("openfiledshotgun", false);
        submachine = sharedPref.getBoolean("submachine", false);
        violet = sharedPref.getBoolean("violet", false);
        Log.e("crimson", Boolean.toString((Boolean) newValue));
        Log.e("darkgreen", Boolean.toString(darkgreen));
        Log.e("openfieldrifle", Boolean.toString(openfieldrifle));
        Log.e("openfiledshotgun", Boolean.toString(openfiledshotgun));
        Log.e("submachine", Boolean.toString(submachine));
        Log.e("violet", Boolean.toString(violet));

        if ((Boolean) newValue || darkgreen || openfieldrifle || openfiledshotgun
                || submachine || violet) {
            return true;
        }
        // If now create a message to the user
        else {
            Toast.makeText(MyPreferencesActivity.this,
                    "Please select at least one video ", Toast.LENGTH_SHORT)
                    .show();
            return false;
        }
    }
};



}

例如:

enter image description here

但这就是我的日志显示!!!

enter image description here

3 个答案:

答案 0 :(得分:3)

你的问题来自这一行:

getPreferenceManager().setSharedPreferencesName("prefs");

如果你删除它,它应该工作。如果你想保留它,那么你应该尝试替换这一行:

    SharedPreferences sharedPref = PreferenceManager
            .getDefaultSharedPreferences(MyPreferencesActivity.this);

通过这个:

SharedPreferences sharedPref = getSharedPreferences("prefs", MODE_PRIVATE);

我希望它可以帮到你。

答案 1 :(得分:2)

我发现您的代码存在两个问题。一个是你只在setOnPreferenceChangeListener上拨打crimsonPreference。您需要为所有Preference个对象执行此操作。第二个是你不能调用从侦听器获取Preference的值,因为该值还没有更新,根据文档:

  

设置当用户更改此首选项时(但在更新内部状态之前)要调用的回调。

答案 2 :(得分:1)

当您从SharedPreferences读取偏好值时,您正在读取先前设置的值(即,在检查或取消选中CheckBoxPreference之前)。

那就是,在你的听众中,你必须确保反转未选中的偏好值(因为那将会发生)。

所以,onPreferenceChange()应该是这样的:

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {

    if ((Boolean)newValue) {
        // Just fall through and enable the option
        return true;
    }

    SharedPreferences sharedPref = PreferenceManager
            .getDefaultSharedPreferences(MyPreferencesActivity.this);
    crimson = sharedPref.getBoolean("crimson", true) // default return value should be true for this
            && !preference.equals(crimsonpreference);
    darkgreen = sharedPref.getBoolean("darkgreen", false)
            && !preference.equals(darkgreenpreference);
    openfieldrifle = sharedPref.getBoolean("openfieldrifle", false)
            && !preference.equals(openfieldriflepreference);
    openfiledshotgun = sharedPref.getBoolean("openfiledshotgun", false)
            && !preference.equals(openfiledshotgunpreference);
    submachine = sharedPref.getBoolean("submachine", false)
            && !preference.equals(submachinepreference);
    violet = sharedPref.getBoolean("violet", false)
            && !preference.equals(violetpreference);

    if (darkgreen || openfieldrifle || openfiledshotgun || submachine || violet) {
        return true;
    }
    // If now create a message to the user
    else {
        Toast.makeText(MyPreferencesActivity.this,
                "Please select at least one video ", Toast.LENGTH_SHORT)
                .show();
        return false;
    }
}

希望这有帮助。