首先,对不起我的英语。
我正在尝试在我的偏好设置屏幕上创建一个自定义的“日期选择器”对话框。我从Android Developer网站和Stackoverflow获得了代码。
对话框“很好”,它保存并加载值(布局包含NumberPicker和TextView)。问题是:设备旋转时对话框消失。
我在这个论坛和其他网站上看过几篇帖子,但没有运气。你能帮助我吗 ?到目前为止,我花了好几个小时来找到问题,但我看不出如何解决它。感谢。
代码:
package hu.test.android.demo.ui;
import hu.test.android.demo.AMdtApplication;
import hu.test.android.demo.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.NumberPicker;
public class DayPreference extends DialogPreference {
private NumberPicker picker = null;
private static int day;
public static int getDay () {
return day;
}
public DayPreference(Context ctxt, AttributeSet attrs) {
super(ctxt, attrs);
setDialogLayoutResource(R.layout.day_preference);
setPositiveButtonText (TestApplication.getContext().getResources().getString( R.string.save ));
setNegativeButtonText (TestApplication.getContext().getResources().getString( R.string.cancel ));
}
@Override
protected void onBindDialogView(View v) {
super.onBindDialogView (v);
picker = (NumberPicker) v.findViewById (R.id.day_preference_number_picker);
picker.setMinValue (1);
picker.setMaxValue (30);
picker.setValue (day);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
if (callChangeListener (day)) {
day = picker.getValue();
persistInt (day);
}
setSummary(getSummary());
}
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return (a.getString(index));
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
if (restoreValue) {
if (defaultValue == null) {
day = getPersistedInt (30);
}
else {
day = getPersistedInt ((Integer) defaultValue);
}
}
else {
day = (Integer) defaultValue;
}
setSummary (getSummary());
}
@Override
public CharSequence getSummary() {
return String.valueOf ( day ) + " " + TestApplication.getContext().getResources().getString( R.string.label_day );
}
// state save & restore
@Override
protected Parcelable onSaveInstanceState () {
final Parcelable superState = super.onSaveInstanceState();
if (isPersistent()) {
return superState;
}
final SavedState myState = new SavedState (superState);
myState.value = day;
return myState;
}
@Override
protected void onRestoreInstanceState (Parcelable state) {
if (state == null || !state.getClass().equals(SavedState.class)) {
super.onRestoreInstanceState(state);
return;
}
SavedState myState = (SavedState) state;
super.onRestoreInstanceState(myState.getSuperState());
// set the value to the picker
picker.setValue (myState.value);
}
private static class SavedState extends BaseSavedState {
public int value;
public SavedState (Parcelable superState) {
super(superState);
}
public SavedState (Parcel source) {
super(source);
value = source.readInt();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeInt(value);
}
// Standard creator object using an instance of this class
@SuppressWarnings( "unused" )
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];
}
};
}
}
答案 0 :(得分:4)
更新:
请注意,下面的解决方案只是一个快速入侵(并不是推荐的方法),正确的解决方案是在onSaveInstanceState期间保持值,然后在重新创建活动后获取这些值。
This post是必须阅读以优雅地处理配置更改。
原始答案:
也许这会有所帮助,请在清单文件中的相关活动代码中添加此行:
android:configChanges="orientation|screenSize"
这将确保在屏幕旋转时不调用onCreate,你对Android操作系统说,嘿我将自己处理旋转的东西,你不需要从onCreate重新开始这项活动。