我正在尝试从自定义TimePreference中获取值。设置mHour和mMinute变量之后,我将它们放在PreferenceFragment.java中的onPreferenceChange中。
对于代码墙感到抱歉,但我想为上下文提供一切。我相信我的问题是在TimePreference.onTimeChanged中发生的,如果你想专注于那里。
我遇到的问题是,每当我获得TimePreference.onTimeChanged中设置的值时,我就会得到之前设置的时间。例如,如果时钟当前设置为下午1:00,我将时间更改为下午3:15,它将读出13:00而不是15:15。
PreferenceFragment.java
TimePreference timePicker = (TimePreference) findPreference("time");
timePicker.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
TimePreference timePreference = (TimePreference) preference;
Long hour = timePreference.getHour();
Long minutes = timePreference.getMinutes();
Log.d(TAG, hour + " " + minutes);
parent.setHour(hour);
parent.setMinutes(minutes);
return true;
}
});
TimePreference.java:
public class TimePreference extends DialogPreference implements TimePicker.OnTimeChangedListener {
private Calendar mCalendar;
private TimePicker picker = null;
private long mHour;
private long mMinutes;
public TimePreference(Context ctxt) {
this(ctxt, null);
}
public TimePreference(Context ctxt, AttributeSet attrs) {
this(ctxt, attrs, android.R.attr.dialogPreferenceStyle);
}
public TimePreference(Context ctxt, AttributeSet attrs, int defStyle) {
super(ctxt, attrs, defStyle);
setPositiveButtonText(R.string.set);
setNegativeButtonText(R.string.cancel);
mCalendar = new GregorianCalendar();
}
public long getHour() {
return mHour;
}
public long getMinutes() {
return mMinutes;
}
@Override
protected View onCreateDialogView() {
picker = new TimePicker(getContext());
picker.setOnTimeChangedListener(this);
return (picker);
}
@Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
picker.setCurrentHour(mCalendar.get(Calendar.HOUR_OF_DAY));
picker.setCurrentMinute(mCalendar.get(Calendar.MINUTE));
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
mCalendar.set(Calendar.HOUR_OF_DAY, picker.getCurrentHour());
mCalendar.set(Calendar.MINUTE, picker.getCurrentMinute());
setSummary(getSummary());
if (callChangeListener(mCalendar.getTimeInMillis())) {
persistLong(mCalendar.getTimeInMillis());
notifyChanged();
}
}
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return (a.getString(index));
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
if (restoreValue) {
long persistedValue;
try {
persistedValue = getPersistedLong(System.currentTimeMillis());
} catch (Exception e) {
//Stale persisted data may be the wrong type
persistedValue = System.currentTimeMillis();
}
mCalendar.setTimeInMillis(persistedValue);
} else if (defaultValue != null) {
mCalendar.setTimeInMillis(Long.parseLong((String) defaultValue));
} else {
//!restoreValue, defaultValue == null
mCalendar.setTimeInMillis(System.currentTimeMillis());
}
setSummary(getSummary());
}
@Override
public CharSequence getSummary() {
if (mCalendar == null) {
return null;
}
return DateFormat.getTimeFormat(getContext()).format(new Date(mCalendar.getTimeInMillis()));
}
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinutes = minute;
}
}