gpt4 book ai didi

android - CheckBoxPreference 不保留选中的值?

转载 作者:行者123 更新时间:2023-11-29 14:50:00 27 4
gpt4 key购买 nike

我有这个偏好

<?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

最佳答案

您的问题来自这一行:

getPreferenceManager().setSharedPreferencesName("prefs");

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

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

通过这个:

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

希望对您有所帮助。

关于android - CheckBoxPreference 不保留选中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21533408/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com