DatePickerDialog setMax()不起作用

时间:2017-04-05 09:56:05

标签: android android-datepicker datepickerdialog

我有以下代码,以setMaxDate()为期5天。但是,只有月份列不合适。它显示月份超过一个月如下图所示。我该怎么解决这个问题?

CODE

Calendar c = Calendar.getInstance();
        c.setTime(new Date(new Date().getTime() + (1000 * 60 * 24)));
        DatePickerDialog datePickerDialog = new DatePickerDialog(ActivityIncompleteSummary.this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        remark.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
                    }
                }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));

        Calendar today = Calendar.getInstance();
        today.add(Calendar.DAY_OF_MONTH, 5);

        datePickerDialog.getDatePicker().setMinDate(new Date().getTime() + (1000 * 60 * 60 * 24));
        datePickerDialog.getDatePicker().setMaxDate(today.getTimeInMillis());
        datePickerDialog.setOnCancelListener(ActivityIncompleteSummary.this);
        datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                if (which == DialogInterface.BUTTON_NEGATIVE) {
                    dialog.dismiss();
                }
            }
        });
        datePickerDialog.show();

屏幕截图 - 添加开始

enter image description here

SCREENSHOT - 滚动日期间

enter image description here

SCREENSHOT - 滚动到最大日

enter image description here

3 个答案:

答案 0 :(得分:0)

更新此c.setTime(new Date(new Date().getTime() + (1000 * 60 * 60 * 24))); 到这一个AstronomycalBody

并确保您当前的日期应大于最小日期且小于最大日期。

答案 1 :(得分:0)

您可以使用以下方法在日历中添加天数

   Calendar c = Calendar.getInstance();
   Log.e("Initial",""+c.getTimeInMillis()); // Print current date timestamp
   c.add(Calendar.DATE,4);
   Log.e("after",""+c.getTimeInMillis());// Print Timestamp with 4+ to current date

然后尝试将PaxDate设置为Picker。

希望它能奏效。

答案 2 :(得分:0)

我遇到了同样的问题。请参阅this

  static TextView DOB;

 // OnClicking the button we are triggering DatePickerFragment Dialog Calender

 public void OnClickDateBirth(View view) {

        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getSupportFragmentManager(), "datePicker");
    }



    public class **DatePickerFragment** extends DialogFragment implements
            DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

            // Use the current date as the default date in the picker

            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH) + 1;
            int day = c.get(Calendar.DAY_OF_MONTH);

            Log.e("DialogMonth", ""+month);

            DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, year, month , day);


           // To Restrict Future Dates

            datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis()); 


            // Create a new instance of DatePickerDialog and return it

              return datePickerDialog;
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            // Do something withdate2 the date chosen by the user

               Log.e("Day", ""+day);
               Log.e("Month", ""+month+1);
               Log.e("Year", ""+year);

               boolean booFuture = validateFutureDate(day, month + 1, year);

                Log.e("BooFuture", ""+booFuture);

                if(!booFuture){

                    Toast.makeText(getApplicationContext(), "Future Dates are not selectable", Toast.LENGTH_SHORT).show();


                }else {

                    String day1=""+day;
                    String month1=""+(month+1);

                    if(day1.length()==1)
                        day1 = "0"+day1;
                    if(month1.length()==1)
                        month1 = "0"+month1;

                    DOB.setText(day1 + "-" + month1 + "-" +year );
                    DOB.setError(null);

                }

        }
    }


   // Restrict future date clickable with following method condition

    public static boolean validateFutureDate(int day,int month,int year){
        final Calendar c = Calendar.getInstance();
        int currentYear = c.get(Calendar.YEAR);
        int currentMonth = c.get(Calendar.MONTH)+1;
        int currentDay = c.get(Calendar.DAY_OF_MONTH);

        Log.e("currentDay", ""+currentDay);
        Log.e("Day", ""+day);

        if (day > currentDay && year == currentYear && month == currentMonth) {


            return false;

        } else if (month > currentMonth && year == currentYear) {


            return false;
        } else if (year > currentYear) {


            return false;
        }

        return true;
    }