這是我要做的事:
public class DatePickerFragment extends DialogFragment {
OnDateSetListener ondateSet;
Calendar c;
int year = 0, month = 0, day = 0;
public DatePickerFragment() {
}
public void setCallBack(OnDateSetListener ondate) {
ondateSet = ondate;
}
public static DatePickerFragment newInstance(Bundle bundle) {
DatePickerFragment myFragment = new DatePickerFragment();
myFragment.setArguments(bundle);
return myFragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// final Calendar c = Calendar.getInstance();
//if else for null arguments
if (getArguments() != null) {
year = getArguments().getInt("year");
month = getArguments().getInt("month");
day = getArguments().getInt("day");
c = Calendar.getInstance();
c.set(year, month, day);
} else {
c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
Log.d("else", "else");
}
DatePickerDialog picker = new DatePickerDialog(getActivity(),
ondateSet, year, month, day);
picker.getDatePicker().setMinDate(c.getTime().getTime());
Log.d("picker timestamp", c.getTime().getTime() + "");
return picker;
}
}
這就是你如何實例化選擇器:
Bundle bundle = new Bundle();
Calendar c = Calendar.getInstance();
bundle.putInt("year", c.get(Calendar.YEAR));
bundle.putInt("month", c.get(Calendar.MONTH));
bundle.putInt("day", c.get(Calendar.DAY_OF_MONTH));
DatePickerFragment fragment = DatePickerFragment
.newInstance(bundle);
fragment.setCallBack(dateSet);
fragment.show(getActivity().getSupportFragmentManager(), null);
這是我的實施隨時可以改變它。請注意,此代碼默認將mindate設置爲currnet日期。 newInstance也很重要。
DatePickerDialog datePickerDialog =新DatePickerDialog(getActivity(),日期,myCalendar.get(Calendar.YEAR) myCalendar.get(的Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)); datePickerDialog.getDatePicker()。setMinDate(System.currentTimeMillis() - 1000); datePickerDialog.show(); – Dilip