嗨!
我有一个实现TimePicker的DialogFragment类。 DialogFragment与PreferenceFragment类绑定,而PreferenceFragment类又由Activity保存。在我的偏好设置中,我还有2个可点击的偏好设置,“设置”和“退出”。我想保存从TimePicker中选取的值仅在按下“设置”按钮时,如果按下“退出”,我想不保存TimePicker中的值,而是使用上次设置的值“设置“被压了。基本上,我想要像默认的Android闹钟一样的行为。
如何做到这一点?我不知道如何从PreferenceFragment类中“取消”我在DialogFragment中获取的值(这是我初始化“Set”和“Exit”按钮的类)
我也在发布代码,但我认为你不必通读它们来回答这个问题。
实现可点击偏好的PreferenceFragment类:
public class PrefsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
Preference setbutton = (Preference)getPreferenceManager().findPreference("set");
if(setbutton != null)
{
setbutton.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference arg0) {
return true;
}
});
}
Preference exitbutton = (Preference)getPreferenceManager().findPreference("close");
if(exitbutton != null)
{
exitbutton.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference arg0) {
return true;
}
});
}
}
实现TimePicker对话框的DialogFragment:
public class TimePreference extends DialogPreference {
private int lastHour=0;
private int lastMinute=0;
private TimePicker picker=null;
private Calendar calendar = Calendar.getInstance();
public static int getHour(String time) {
String[] pieces=time.split(":");
return(Integer.parseInt(pieces[0]));
}
public static int getMinute(String time) {
String[] pieces=time.split(":");
return(Integer.parseInt(pieces[1]));
}
public TimePreference(Context ctxt) {
this(ctxt, null);
}
public TimePreference(Context ctxt, AttributeSet attrs) {
this(ctxt, attrs, 0);
}
public TimePreference(Context ctxt, AttributeSet attrs, int defStyle) {
super(ctxt, attrs, defStyle);
setPositiveButtonText("Set");
setNegativeButtonText("Cancel");
}
@Override
protected View onCreateDialogView() {
picker=new TimePicker(getContext());
return(picker);
}
@Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
picker.setCurrentHour(lastHour);
picker.setCurrentMinute(lastMinute);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
lastHour=picker.getCurrentHour();
lastMinute=picker.getCurrentMinute();
String time=String.valueOf(lastHour)+":"+String.valueOf(lastMinute);
if (callChangeListener(time)) {
persistString(time);
}
setSummary(time);
}
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return(a.getString(index));
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
String time=null;
if (restoreValue) {
if (defaultValue==null) {
time=getPersistedString("00:00");
}
else {
time=getPersistedString(defaultValue.toString());
}
}
else {
time=defaultValue.toString();
}
lastHour=getHour(time);
lastMinute=getMinute(time);
setSummary(time);
}
}
非常感谢!