我面临一个奇怪的问题,我创建了一个自定义的DatePickerDialog来维护上一个选定的日期,但不幸的是每次都设置了setMaxDate日期。当我点击show the datepicker对话框时: - (
public class CustomDatePickerDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener {
int currentYear = -1, currentMonth = -1, currentDate = -1;
public static String MONTH = "month";
public static String YEAR = "year";
public static String DAY = "day";
private DatePickerListener listener;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar calendar = Calendar.getInstance();
currentYear = getArguments().getInt(YEAR);
currentMonth = getArguments().getInt(MONTH);
currentDate = getArguments().getInt(DAY);
if (currentYear == -1) {
currentYear = calendar.get(Calendar.YEAR);
}
if (currentMonth == -1) {
currentMonth = calendar.get(Calendar.MONTH);
}
if (currentDate == -1) {
currentDate = calendar.get(Calendar.DATE);
}
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, currentYear, currentMonth, currentDate);
Date date = calendar.getTime();
datePickerDialog.getDatePicker().setMaxDate(date.getTime());
return datePickerDialog;
}
public void setListener(DatePickerListener listener) {
this.listener = listener;
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
currentYear = year;
currentMonth = monthOfYear;
currentDate = dayOfMonth;
listener.onDateSet(year, monthOfYear, dayOfMonth);
}
public interface DatePickerListener {
void onDateSet(int year, int month, int day);
}
}
我的OnClickListener:
edtSubstrateChanged.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openCalendarDailog(edtSubstrateChanged.getText().toString(), SUBSTRATE);
}
});
打开dailog的方法:
public void openCalendarDailog(String strDate, int clickedEditext) {
int day = -1, month = -1, year = -1;
if (!strDate.isEmpty()) {
String[] strings = strDate.split("-");
day = Integer.parseInt(strings[0]);
month = Integer.parseInt(strings[1]);
year = Integer.parseInt(strings[0]);
}
this.clickedEditext = clickedEditext;
Bundle bundle = new Bundle();
bundle.putInt(CustomDatePickerDialog.DAY, day);
bundle.putInt(CustomDatePickerDialog.MONTH, month);
bundle.putInt(CustomDatePickerDialog.YEAR, year);
mCustomDatePickerDialog = new CustomDatePickerDialog();
mCustomDatePickerDialog.setListener(this);
mCustomDatePickerDialog.setArguments(bundle);
mCustomDatePickerDialog.show(getSupportFragmentManager().beginTransaction(), "Date");
}