0

我有 2 个 DatePicker,一个是开始日期选择器,另一个是结束日期选择器。我想将结束 DatePicker 的最小日期设置为用户选择的开始日期。

我设置了用户在 Calander startDate 中选择的数据,并在结束日期将此数据设置为 MinDate。我已经尝试过这种方式,但它不起作用:(

我不能使用Mateiral Range Date Picker,因为我必须使用特定的主题。另外我不擅长编程,所以如果你能详细解释一下,我将不胜感激。

ipDcEventStartDay.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v){
        final Calendar c = Calendar.getInstance();

        startYear = c.get(Calendar.YEAR);
        startMonth = c.get(Calendar.MONTH);
        startDay = c.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                startYear = year;
                startMonth = month+1;
                startDay = dayOfMonth;

                ipDcEventStartDay.setText(startYear + " - " + startMonth + " - " + startDay);
                ipDcEventStartDay.setTextColor(Color.parseColor("#000000"));
            }
        }, startYear, startMonth, startDay);
        dpd.show();
    }
});

Calendar startDate = Calendar.getInstance();
startDate.set(Calendar.YEAR, startYear);
startDate.set(Calendar.MONTH, startMonth);
startDate.set(Calendar.DAY_OF_MONTH, startDay);

ipDcEventEndDay.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final Calendar c = Calendar.getInstance();

        endYear = c.get(Calendar.YEAR);
        endMonth = c.get(Calendar.MONTH);
        endDay = c.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                endYear = year;
                endMonth = month;
                endDay = dayOfMonth;

                ipDcEventEndDay.setText(endYear + " - " + (endMonth+1) + " - " + endDay);
                ipDcEventEndDay.setTextColor(Color.parseColor("#000000"));
            }
        }, endYear,endMonth, endDay);
        dpd.getDatePicker().setMinDate(startDate.getTimeInMillis());
        dpd.show();
    }
});
4

1 回答 1

0

这段代码:

startDate.set(Calendar.YEAR, startYear);
startDate.set(Calendar.MONTH, startMonth);
startDate.set(Calendar.DAY_OF_MONTH, startDay);

应该在里面

ipDcEventEndDay.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
}

因为startYear,startMonthstartDay仅设置为 on ipDcEventStartDay.setOnClickListener(),所以一开始它们有一些价值,但不是设置为 on 的值ipDcEventStartDay.setOnClickListener()

因此,ipDcEventEndDay.setOnClickListener()您必须再次设置startDate在另一个上选择的值DatePickerDialog

于 2021-04-13T16:52:55.657 回答