如何根据datepicker android studio设置max timepicker limit

时间:2018-03-21 03:45:10

标签: android-studio datetimepicker timepicker

如何在特定日期设置限制最大时间戳? 当我在datepicker中选择星期六,然后时间选择器设置在12.00中限制最大小时。但是如果我在其他日子选择了datepicker,那么时间选择器会在15.00(正常)中限制最大小时数。我使用这个波纹管代码却失败了,当我选择星期六总是假的。

   if( calendarToday.get( Calendar.DAY_OF_WEEK ) == Calendar.SATURDAY){

日期选择器()

public void datePicker() {
   final Calendar calendarMax = Calendar.getInstance();
   calendarMax.add(Calendar.DAY_OF_MONTH, 7);

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

   DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(FormBooking.this, year, month, day);
   datePickerDialog.setMinDate(calendarToday);
   datePickerDialog.setMaxDate(calendarMax);

   //sunday off
   List<Calendar> daysList = new LinkedList<>();
   Calendar[] daysArray;
   Calendar cAux = Calendar.getInstance();

   while( calendarToday.getTimeInMillis() <= calendarMax.getTimeInMillis() ){
       if( calendarToday.get( Calendar.DAY_OF_WEEK ) != Calendar.SUNDAY){
           Calendar c = Calendar.getInstance();
           c.setTimeInMillis( calendarToday.getTimeInMillis() );
           daysList.add( c );
       }
       calendarToday.setTimeInMillis( calendarToday.getTimeInMillis() + ( 24 * 60 * 60 * 1000 ) );
   }
   daysArray = new Calendar[ daysList.size() ];
   for( int i = 0; i < daysArray.length; i++ ){
       daysArray[i] = daysList.get(i);
   }
   datePickerDialog.setSelectableDays( daysArray );

   datePickerDialog.vibrate(true);
   datePickerDialog.show( getFragmentManager(), "DatePickerDialog" );
}

onDataSet()

public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
   int bulan = (++monthOfYear);
   String day_string = String.valueOf(dayOfMonth);
   String month_string = String.valueOf(bulan);
   if (dayOfMonth < 10) {
      day_string = "0" + String.valueOf(dayOfMonth);
   }
   if (bulan < 10) {
      month_string = "0" + String.valueOf(bulan);
   }

   Calendar selectedDate =  Calendar.getInstance();
   int mYear   = selectedDate.get(Calendar.YEAR);
   int mMonth  = selectedDate.get(Calendar.MONTH);
   int mDay    = selectedDate.get(Calendar.DAY_OF_MONTH);
   String D    = String.valueOf(mDay);

   if(String.valueOf(dayOfMonth).equals(D)){    //if choose today
       if(selectedDate.get(Calendar.HOUR_OF_DAY)>6 && selectedDate.get(Calendar.HOUR_OF_DAY)<15) {
           if(selectedDate.get(Calendar.HOUR_OF_DAY)==14 && selectedDate.get(Calendar.MINUTE)>30) {
               input_time.setText("");
           } else {
               timePicker(true);
           }
       }else if(selectedDate.get(Calendar.HOUR_OF_DAY)>0 && selectedDate.get(Calendar.HOUR_OF_DAY)<7) {
           timePicker(true);
       }else{
           input_time.setText("");
       }
   }else{
       timePicker(false);
   }

   input_date.setText("");
   input_date.setText(day_string + "." + month_string + "." + year);
}

timePicker()

public void timePicker(boolean isTodayDate){
   int minHour = 8;
   int maxHour = 15;    //max hour, if saturday is 12, but other day, normal maxHour 15
   Calendar now = Calendar.getInstance();
   TimePickerDialog timepickerdialog = TimePickerDialog.newInstance(
        FormBooking.this,
        now.get(Calendar.HOUR_OF_DAY),
        now.get(Calendar.MINUTE),
        true);

   if(isTodayDate){ //if choose today
       if(now.get(Calendar.HOUR_OF_DAY)>6 && now.get(Calendar.HOUR_OF_DAY)<15){
           if(now.get(Calendar.MINUTE)>=0 && now.get(Calendar.MINUTE)<61) {
               minHour = now.get(Calendar.HOUR_OF_DAY) + 1;
           }
       }else if(now.get(Calendar.HOUR_OF_DAY)>0 && now.get(Calendar.HOUR_OF_DAY)<7){
           minHour = 8;
       }
   }

   timepickerdialog.setMinTime(minHour, 0, 0);
   timepickerdialog.setMaxTime(maxHour, 0, 0);

   timepickerdialog.setThemeDark(false);
   timepickerdialog.vibrate(false);
   timepickerdialog.dismissOnPause(false);
   timepickerdialog.show(getFragmentManager(), "Timepickerdialog");
}

1 个答案:

答案 0 :(得分:2)

您可以使用以下代码检查所选日期是否为星期六:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, monthOfYear);// you have increased value of monthOfYear by using ++monthOfYear so you have to use monthOfYear-1
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
isSaturday = cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY;

并将此布尔值传递给timePicker(false,isSaturday)函数。在timePicker函数中,您可以设置maxHours:

int maxHour = 15;
if(isSaturday){ 
    maxHour=12 
}