只是在这里学习android代码。我不确定我错过了什么,但我正在尝试创建一个DialogPreference
使用 a 的自定义,NumberPicker
它可以让用户选择年份。我尝试遵循 android's Settings Guide,但我选择的值没有保存。只要我不退出应用程序,它就会保存,但如果我退出,那么当我重新启动应用程序时它会重置为默认值。我错过了什么?
设置活动代码:
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);
}
}
term_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>