0
編輯:我試過這個新的代碼,但它仍然似乎他們被重置。任何人都可以告訴我,並提出解決方案,我的設置被重置?偏好活動設置不保存
public class UserSettingActivity extends PreferenceActivity implements OnPreferenceChangeListener{
static SwitchPreference mucus_stamps;
static SwitchPreference fertile_infertil;
static SwitchPreference cervical_mucus;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
/**
* Populate the activity with the top-level headers.
*/
@Override
public void onBuildHeaders(List<Header> target) {
Log.i("PROJECTCARUSO","onBuildHeaders");
loadHeadersFromResource(R.xml.preference_headers, target);
}
/**
* This fragment shows the preferences for the first header.
*/
public static class Prefs1Fragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.notification_settings);
}
}
/**
* This fragment shows the preferences for the second header.
*/
public static class Prefs2Fragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.charting_settings);
//findPreference("cervical_mucus").setOnPreferenceChangeListener(
mucus_stamps = (SwitchPreference) findPreference("mucus_stamps");
fertile_infertil = (SwitchPreference) findPreference("fertile_infertil");
cervical_mucus = (SwitchPreference) findPreference("cervical_mucus");
cervical_mucus.setOnPreferenceChangeListener(
new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// string test w/o "==" usage.
if (newValue.equals(false)) {
Log.i("PROJECTCARUSO","false");
mucus_stamps.setChecked(false);
fertile_infertil.setChecked(false);
} else {
Log.i("PROJECTCARUSO","true");
mucus_stamps.setChecked(true);
fertile_infertil.setChecked(true);
}
// true instead of false so the new value gets kept
return true;
};
});
if(!cervical_mucus.isChecked()){
mucus_stamps.setSelectable(false);
mucus_stamps.setEnabled(false);
fertile_infertil.setSelectable(false);
fertile_infertil.setEnabled(false);
} else {
mucus_stamps.setSelectable(true);
mucus_stamps.setEnabled(true);
fertile_infertil.setSelectable(true);
fertile_infertil.setEnabled(true);
}
}
}
protected void onPause()
{
super.onPause();
}
protected void onResume()
{
super.onResume();
}
@Override
public boolean onPreferenceChange(Preference arg0, Object arg1) {
// TODO Auto-generated method stub
return true;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="@string/pref_chart_profile"
android:textSize="20px">
<SwitchPreference
android:title="@+string/pref_symptothermal"
android:summary="@+string/pref_symptothermal_summary"
android:key="symptothermal"
android:defaultValue="true"
android:layout="@layout/pref_layout"/>
<SwitchPreference
android:id="@+id/cervical_mucus"
android:title="@+string/pref_cervical_mucus"
android:summary="@+string/pref_cervical_mucus_summary"
android:key="cervical_mucus"
android:defaultValue="true"
android:layout="@layout/pref_layout" />
<SwitchPreference
android:id="@+id/mucus_stamps"
android:title="@+string/pref_mucus_stamps"
android:summary="@+string/pref_mucus_stamps_summary"
android:key="mucus_stamps"
android:defaultValue="true"
android:layout="@layout/pref_layout" />
<SwitchPreference
android:id="@+id/fertile_infertil"
android:title="@+string/pref_fertile_infertile"
android:summary="@+string/pref_fertile_infertile_summary"
android:key="fertile_infertil"
android:defaultValue="true"
android:layout="@layout/pref_layout" />
</PreferenceCategory>
</PreferenceScreen>
進行了編輯,但仍然重新設置,當我回到它。看到我上面的新代碼 – jcaruso