gpt4 book ai didi

android - 使用 NumberPicker 作为 DialogPreference。为什么我的设置不保存?

转载 作者:太空狗 更新时间:2023-10-29 13:24:29 25 4
gpt4 key购买 nike

只是在这里学习android代码。我不确定我遗漏了什么,但我正在尝试创建一个自定义 DialogPreference,它使用 NumberPicker 让用户选择年份。我试着关注安卓的 Settings Guide ,但我选择的值不会保存。只要我不退出应用程序,它就会保存,但如果我退出,那么当我重新启动应用程序时,它会重置为默认值。我错过了什么?

设置 Activity 代码:

public class SettingsActivity extends PreferenceActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();

}

public static class MyPreferenceFragment extends PreferenceFragment
{
@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.app_preferences);
}
}
}

app_preferences.xml(SettingsActivity 的布局)

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

<com.NsouthProductions.gradetrackerpro.SemesterPickerPreference
android:key="MY_PICKER"
android:title="@string/year_pref_str"
android:summary="Choose a year here"
android:defaultValue="2005"
/>

</PreferenceScreen>

SemesterPickerPreference(DialogPreference 的扩展)

public class SemesterPickerPreference extends DialogPreference {

private int year;
private NumberPicker semPick;
private int DEFAULT_VALUE;

public SemesterPickerPreference(Context context, AttributeSet attrs) {
super(context, attrs);

DEFAULT_VALUE = 2003;

setDialogLayoutResource(R.layout.semester_picker_dialog);
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);

setDialogIcon(null);
}

@Override
protected void onDialogClosed(boolean positiveResult) {

if (positiveResult) {
persistInt(semPick.getValue());
}
}

@Override
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
if (restorePersistedValue) {
// Restore existing state
year = this.getPersistedInt(DEFAULT_VALUE);
} else {
// Set default state from the XML attribute
year = (Integer) defaultValue;
persistInt(year);
}
}

@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getInteger(index, DEFAULT_VALUE);
}

@Override
protected View onCreateDialogView() {

int max = 2038;
int min = 2001;

LayoutInflater inflater =
(LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.semester_picker_dialog, null);

semPick = (NumberPicker) view.findViewById(R.id.semester_picker);

// Initialize state
semPick.setMaxValue(max);
semPick.setMinValue(min);
semPick.setValue(this.getPersistedInt(DEFAULT_VALUE));
semPick.setWrapSelectorWheel(false);

return view;
}



// This code copied from android's settings guide.

private static class SavedState extends BaseSavedState {
// Member that holds the setting's value
// Change this data type to match the type saved by your Preference
int value;

public SavedState(Parcelable superState) {
super(superState);
}

public SavedState(Parcel source) {
super(source);
// Get the current preference's value
value = source.readInt(); // Change this to read the appropriate data type
}

@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
// Write the preference's value
dest.writeInt(value); // Change this to write the appropriate data type
}

// Standard creator object using an instance of this class
public static final Parcelable.Creator<SavedState> CREATOR =
new Parcelable.Creator<SavedState>() {

public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}

public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}



@Override
protected Parcelable onSaveInstanceState() {
final Parcelable superState = super.onSaveInstanceState();
// Check whether this Preference is persistent (continually saved)
if (isPersistent()) {
// No need to save instance state since it's persistent, use superclass state
return superState;
}

// Create instance of custom BaseSavedState
final SavedState myState = new SavedState(superState);
// Set the state's value with the class member that holds current setting value
myState.value = year;
return myState;
}

@Override
protected void onRestoreInstanceState(Parcelable state) {
// Check whether we saved the state in onSaveInstanceState
if (state == null || !state.getClass().equals(SavedState.class)) {
// Didn't save the state, so call superclass
super.onRestoreInstanceState(state);
return;
}

// Cast state to custom BaseSavedState and pass to superclass
SavedState myState = (SavedState) state;
super.onRestoreInstanceState(myState.getSuperState());

// Set this Preference's widget to reflect the restored state
semPick.setValue(myState.value);
}
}

semester_picker_dialog.xml(我的学期/年级选择器的布局)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_horizontal" >

<NumberPicker
android:id="@+id/semester_picker"
android:layout_centerInParent="true"
android:layout_width="100dip"
android:layout_height="match_parent" />

</LinearLayout>

最佳答案

你错过了 android:persistent="true"您首选项 XML 中的 XML 属性。这就是告诉首选项是否实际将其值存储到共享首选项的原因。调用 PersistInt() 显然是不够的。

关于android - 使用 NumberPicker 作为 DialogPreference。为什么我的设置不保存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23038030/

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