Android - 为开始日期和结束日期多次更新 DatePickers 的 minDate 和 maxDate

Android - Update minDate and maxDate of a DatePickers many times for Start date and End date

我有两个用于 ToDate[start] 和 FromDate[end] 的日期选择器对话框。

我正在使用 setMaxDate() 函数将 datePicker 最大日期设置为当前日期。

我的问题是

1) 我要更改 ToDatePicker 的日期 我不会设置 FromDatePicker 的 setMaxDate()。

2) 我要更改 FromDatePicker 的日期 我不会设置 ToDatePicker 的 setMinDate()。

在我的代码中,这是第一次工作,但下次我同时更改或更改单个日期选择器时,它没有设置 setMaxDate()、setMinDate() 函数

这是我的代码

dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
    Calendar newCalendar = Calendar.getInstance();

    //Setting first None value to updatedDate To and from
    Util.SaveToSharedPref(MgntNCActivity.this,
            Constant.ShaPreMgntNcFilter,
            Constant.ShaPreMgntNcFilterUpdatedToDate, Constant.NoValue);
    Util.SaveToSharedPref(MgntNCActivity.this,
            Constant.ShaPreMgntNcFilter,
            Constant.ShaPreMgntNcFilterUpdatedFromDate, Constant.NoValue);

    datePickerDiaFrom=new DatePickerDialog(MgntNCActivity.this, new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            Calendar upDateFrom = Calendar.getInstance();
            upDateFrom.set(year, monthOfYear, dayOfMonth);
            txtFromUpdatedDate.setText(dateFormatter.format(upDateFrom.getTime()));
            // System.out.println("Date 1 )GTM : "+upDateTo.getTime().toGMTString());

         //setting minimum limit to date Picker 'To'
           if (dateFormatter.format(upDateFrom.getTime()).equals(dateFormatter.format(NetworkUtil.getTodayDate()))) {
                upDateFrom.set(Calendar.HOUR_OF_DAY, upDateFrom.getMinimum(Calendar.HOUR_OF_DAY));
                upDateFrom.set(Calendar.MINUTE, upDateFrom.getMinimum(Calendar.MINUTE));
                upDateFrom.set(Calendar.SECOND, upDateFrom.getMinimum(Calendar.SECOND));
                upDateFrom.set(Calendar.MILLISECOND, upDateFrom.getMinimum(Calendar.MILLISECOND));

                datePickerDiaTo.getDatePicker().setMinDate(upDateFrom.getTimeInMillis());

                System.out.println("setting To DatePicker current date as minDate : "
                                            +dateFormatter.format(NetworkUtil.getTodayDate()));
            }else{
                datePickerDiaTo.getDatePicker().setMinDate(upDateFrom.getTimeInMillis());
                System.out.println("setting To DatePicker minDate : "
                            +dateFormatter.format(upDateFrom.getTime()));

            }

           //datePickerDiaTo.getDatePicker().setMinDate(upDateFrom.getTimeInMillis());

            Util.SaveToSharedPref(MgntNCActivity.this,
                    Constant.ShaPreMgntNcFilter,
                    Constant.ShaPreMgntNcFilterUpdatedFromDate, dateFormatter.format(upDateFrom.getTime()));
        }
    }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

    datePickerDiaTo=new DatePickerDialog(MgntNCActivity.this, new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            Calendar upDateTo = Calendar.getInstance();
            upDateTo.set(year, monthOfYear, dayOfMonth);
            txtToUpdatedDate.setText(dateFormatter.format(upDateTo.getTime()));
            // System.out.println("Date 1 )GTM : "+upDateTo.getTime().toGMTString());

         //setting maximum limit to date Picker 'From'
           if (dateFormatter.format(upDateTo.getTime()).equals(dateFormatter.format(NetworkUtil.getTodayDate()))) {
              upDateTo.set(Calendar.HOUR_OF_DAY, upDateTo.getMaximum(Calendar.HOUR_OF_DAY));
              upDateTo.set(Calendar.MINUTE, upDateTo.getMaximum(Calendar.MINUTE));
              upDateTo.set(Calendar.SECOND, upDateTo.getMaximum(Calendar.SECOND));
              upDateTo.set(Calendar.MILLISECOND, upDateTo.getMaximum(Calendar.MILLISECOND));

              datePickerDiaFrom.getDatePicker().setMaxDate(upDateTo.getTimeInMillis());

             System.out.println("setting From DatePicker current date as maxDate : "
                        +dateFormatter.format(NetworkUtil.getTodayDate()));
            }else{
                datePickerDiaFrom.getDatePicker().setMaxDate(upDateTo.getTimeInMillis());
                System.out.println("setting From DatePicker maxDate : "
                        +dateFormatter.format(upDateTo.getTime()));
            }

           //datePickerDiaFrom.getDatePicker().setMaxDate(upDateTo.getTimeInMillis());

            Util.SaveToSharedPref(MgntNCActivity.this,
                    Constant.ShaPreMgntNcFilter,
                    Constant.ShaPreMgntNcFilterUpdatedToDate, dateFormatter.format(upDateTo.getTime()));

        }
    }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

    //setting max limit to date Picker
    datePickerDiaTo.getDatePicker().setMaxDate(NetworkUtil.getTodayDate().getTime());
    datePickerDiaFrom.getDatePicker().setMaxDate(NetworkUtil.getTodayDate().getTime());

任何人请建议我解决这个... 谢谢...

终于找到解决办法了:)

我正在写 datePickerDialogs 'datePickerDiaFrom','datePickerDiaTo' 是 class 变量,这就是为什么我当时更改 minDate 或 maxDate 时它不会替换以前的日期。

我的解决方案是,

每次我点击 textview 打开新的 dialogPicker 并为其设置范围。

这是我的代码:

static long MaximumDate=0,MinimunDate=0;

dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);

    View.OnClickListener showDatePicker = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final View vv = v;

            Calendar newCalendar=Calendar.getInstance();    

            switch (vv.getId()) {
            case R.id.txtFromUpdatedDate_MgntNC:

                DatePickerDialog dialogFrom=new DatePickerDialog(MgntNCActivity.this, new OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year,
                            int monthOfYear, int dayOfMonth) {
                        Calendar upDateFrom = Calendar.getInstance();
                        upDateFrom.set(year, monthOfYear, dayOfMonth);
                        txtFromUpdatedDate.setText(dateFormatter.format(upDateFrom.getTime()));

                        MinimunDate=upDateFrom.getTimeInMillis();


                    }

                }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));


                if (MaximumDate!=0) {
                    dialogFrom.getDatePicker().setMaxDate(MaximumDate);
                }else{
                    dialogFrom.getDatePicker().setMaxDate(NetworkUtil.getTodayDate().getTime());
                }

                dialogFrom.show();

                dialogFrom.setOnCancelListener(new DialogInterface.OnCancelListener() {

                    @Override
                    public void onCancel(DialogInterface arg0) {
                        MinimunDate=0;

                         txtFromUpdatedDate.setText(Constant.NoValue);
                    }
                });

                break;

            case R.id.txtToUpdatedDate_MgntNC:
                DatePickerDialog dialogTo=new DatePickerDialog(MgntNCActivity.this, new OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year,
                            int monthOfYear, int dayOfMonth) {
                        Calendar upDateTo = Calendar.getInstance();
                        upDateTo.set(year, monthOfYear, dayOfMonth);
                        txtToUpdatedDate.setText(dateFormatter.format(upDateTo.getTime()));

                        MaximumDate=upDateTo.getTimeInMillis();

                    }

                }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

                dialogTo.getDatePicker().setMaxDate(NetworkUtil.getTodayDate().getTime());
                if (MinimunDate!=0) {
                    dialogTo.getDatePicker().setMinDate(MinimunDate);
                }else{
                    //dialog1.getDatePicker().setMinDate(NetworkUtil.getTodayDate().getTime());
                }

                dialogTo.show();

                dialogTo.setOnCancelListener(new DialogInterface.OnCancelListener() {

                    @Override
                    public void onCancel(DialogInterface arg0) {
                        MaximumDate=0;

                    }
                });


                break;  

            default:
                break;
            }



        }
    };

    txtFromUpdatedDate.setOnClickListener(showDatePicker);
    txtToUpdatedDate.setOnClickListener(showDatePicker);