如何在 android 日期选择器中将特定选定日期设置为最小日期? [复制]

Posted

技术标签:

【中文标题】如何在 android 日期选择器中将特定选定日期设置为最小日期? [复制]【英文标题】:How can I set the specific selected date to min date in android date picker? [duplicate] 【发布时间】:2021-07-08 12:15:25 【问题描述】:

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

我尝试过这种方式,但它不起作用:(

    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();
        
    );

【问题讨论】:

您可以尝试使用MaterialDatePicker。它适用于范围。 【参考方案1】:

注意::TextView 保存要设置日期的文本的引用

private fun openDobPicker(textView: TextView) 
            val c = Calendar.getInstance()
            val year = c.get(Calendar.YEAR)
            val month = c.get(Calendar.MONTH)
            val day = c.get(Calendar.DAY_OF_MONTH)
    
            val dpd = DatePickerDialog(requireActivity(), DatePickerDialog.OnDateSetListener  view, year, monthOfYear, dayOfMonth ->
                val date = "$dayOfMonth/$monthOfYear/$year"
                textView.text = date
            , year, month, day)
    
            //restrict calendar to show future dates
            dpd.datePicker.maxDate = Date().time
            
            //limit the minimum date then do this
            dpd.datePicker.minDate = PASSED_MIN_DATE_HERE
        
    
            
            dpd.show()
        

【讨论】:

以上是关于如何在 android 日期选择器中将特定选定日期设置为最小日期? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

在引导日期选择器中将课程添加到多个/特定日期?

如何在android的日期选择器中设置特定日期

如何在前一个 tableview 控制器的 detailtext 标签中显示选定的选择器日期

在android中将日期设置为日期选择器

如何在 jquery ui 日期选择器中同时禁用周末和选定日期?

在 Android 日期选择器中禁用特定日期