如何在Android日期选择器中将特定选择的日期设置为最小日期? [英] How can I set the specific selected date to min date in android date picker?

查看:82
本文介绍了如何在Android日期选择器中将特定选择的日期设置为最小日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 2 DatePickers, one is start date picker and the other is end date picker. I want to set the min date of the end DatePicker to the start date which user has selected.

我已经尝试过这种方法,但是它不起作用:(

I've tried in this way but it's not working :(

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

推荐答案

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

Note:: TextView hold the reference of text where you want to set the date

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日期选择器中将特定选择的日期设置为最小日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆