DatePicker的MinDate是否有问题?

时间:2019-07-30 12:44:21

标签: android android-layout datepicker android-calendar

设置后:

ERROR: Job failed: exit code 1

我的布局看起来不正确,日期显示在我的日历顶部:

enter image description here enter image description here

我尝试设置 maxDate ,而不设置任何条件,它按原样显示了布局。 问题出在 minDate

3 个答案:

答案 0 :(得分:0)

  final Calendar c = Calendar.getInstance();  
    int year = c.get(Calendar.YEAR);  
    int month = c.get(Calendar.MONTH);  
    int day = c.get(Calendar.DAY_OF_MONTH);  
DatePickerDialog dpd = new DatePickerDialog(getActivity(),AlertDialog.THEME_TRADITIONAL,this,year, month, day);  

    //Get the DatePicker instance from DatePickerDialog  
    DatePicker dp = dpd.getDatePicker();  
    //Set the DatePicker minimum date selection to current date  
    dp.setMinDate(c.getTimeInMillis());//get the current day  
    //dp.setMinDate(System.currentTimeMillis() - 1000);// Alternate way to get the current day  

    //Add 6 days with current date  
    c.add(Calendar.DAY_OF_MONTH,6);  

    //Set the maximum date to select from DatePickerDialog  
    dp.setMaxDate(c.getTimeInMillis());  
    //Now DatePickerDialog have 7 days range to get selection any one from those dates 

答案 1 :(得分:0)

尝试一下

 val cal = Calendar.getInstance()
 val now = Calendar.getInstance()
 val datePickerDialog = com.wdullaer.materialdatetimepicker.date.DatePickerDialog.newInstance(
               activity,
                now.get(Calendar.YEAR),
                now.get(Calendar.MONTH),
                now.get(Calendar.DAY_OF_MONTH))
 datePickerDialog.setMinDate(cal)

答案 2 :(得分:0)

尝试下面的代码,这应该可以解决您的问题

    Calendar newCalendar = Calendar.getInstance();
DatePickerDialog toDatePickerDialog = new DatePickerDialog(Record_Sample_Collection_Details.this, new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        Calendar newDate = Calendar.getInstance();
        newDate.set(year, monthOfYear, dayOfMonth);                                                       
    }

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

toDatePickerDialog.getDatePicker().setMinDate(newCalendar.getTimeInMillis());