【问题标题】:How to disable the previous dates in date picker dialogue box [duplicate]如何在日期选择器对话框中禁用以前的日期[重复]
【发布时间】:2016-08-26 07:46:21
【问题描述】:

我是一名学生,我正在开发一个 android 预订应用程序。在这些应用程序中,我有一个选择日期的按钮,当我点击该按钮时,会出现一个选择日期的对话框,但我想 禁用所有以前的日期,只想显示当前日期和一个月的另外 3 个日期。请帮助我如何做到这一点,谢谢

pPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });
    final Calendar cal = Calendar.getInstance();
    pYear = cal.get(Calendar.YEAR);
    pMonth = cal.get(Calendar.MONTH);
    pDay = cal.get(Calendar.DAY_OF_MONTH);

    /** Display the current date in the TextView */
    updateDisplay();

}
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,
                    pDateSetListener,
                    pYear, pMonth, pDay);
    }
    return null;
}

private void updateDisplay() {
    pDisplayDate.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(pMonth + 1).append("/")
                    .append(pDay).append("/")
                    .append(pYear).append(" "));

}

【问题讨论】:

    标签: java android xml


    【解决方案1】:

    您可以为 DatePicker 设置最小和最大日期,请执行以下操作:

    yourDatePickerDialog.getDatePicker().setMinDate(youMinDate);
    

    https://developer.android.com/reference/android/widget/DatePicker.html#setMinDate(long)

    【讨论】:

      【解决方案2】:

      试试这个

      DatePickerDialog datePicker;
       private Calendar calendar;
      private int year, month, day;
            // FETCH YEAR,MONTH,DAY FROM CALENDAR
         calendar = Calendar.getInstance();
          year = calendar.get(Calendar.YEAR);
          month = calendar.get(Calendar.MONTH);
          day = calendar.get(Calendar.DAY_OF_MONTH);
      
      datePicker = new DatePickerDialog(this, YOUR_LISTENER, year, month, day);
      // this line is used for disable previous date but u can select the date
      datePicker.getDatePicker().setMinDate(System.currentTimeMillis());
      // this line is used to prevent date selection 
       datePicker.getDatePicker().setCalendarViewShown(false);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-16
        • 1970-01-01
        • 2014-03-23
        • 2016-04-16
        • 2023-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多