2016-02-24 141 views
9

我使用的日期選擇器,我可以通過下面的代碼後30天禁止目前和以後的日子裏的最後幾天:禁用特定日期

DatePickerDialog datePicker = new DatePickerDialog(); 

      Calendar calender = Calendar.getInstance(); 
      long today = calender.getTimeInMillis(); 
      final long oneDay = 24 * 60 * 60 * 1000L; 

      Date previousDays = new Date(today - 1000); 
      datePicker.setMinDate(DateToCalendar(previousDays)); 

      Date nextMonth = new Date(today + 30 * oneDay); 
      datePicker.setMaxDate(DateToCalendar(nextMonth)); 

如果我想禁用週五每個月,我該怎麼做?

回答

5

你可以使用這個庫Material Date Time Picker,在這裏你可以設置一個選項,以顯示特定日期,例如:

datePicker.setSelectableDays(Calendar[] days) 

又通的Calendar數組包含了所有可選日期的參數。

+1

非常感謝,這個偉大的工程。我不知道爲什麼標準的android之一是如此有限。 – Zapnologica

+0

如何通過上述庫中的日期禁用?假設如何禁用所有周末? – prateek31

+0

創建不包含週末的日期數組,並將其作爲可選日期傳遞。 – himanshu1496

4

這對於android datepicker來說是不可能的,您需要爲自己創建一個自定義選擇器。見MaterialDateTimePicker

要禁用週日,你必須傳遞數組這樣

 GregorianCalendar g1=new GregorianCalendar(); 
     g1.add(Calendar.DATE, 1); 
     GregorianCalendar gc = new GregorianCalendar(); 
     gc.add(Calendar.DAY_OF_MONTH, 30); 
     List<Calendar> dayslist= new LinkedList<Calendar>(); 
Calendar[] daysArray; 
Calendar cAux = Calendar.getInstance(); 
while (cAux.getTimeInMillis() <= gc.getTimeInMillis()) { 
    if (cAux.get(Calendar.DAY_OF_WEEK) != 1) { 
     Calendar c = Calendar.getInstance(); 
     c.setTimeInMillis(cAux.getTimeInMillis()); 
     dayslist.add(c); 
    } 
    cAux.setTimeInMillis(cAux.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); 
+3

使用Calendar.SUNDAY而不是1 – dilix

+0

代碼中的gc是什麼? –

+0

代碼中的gc是什麼? –

2

使用自定義DatePickerDialog

中的build.gradle添加此

編譯 'com.wdullaer:materialdatetimepicker:3.5.1'

實施

DatePickerDialog.OnDateSetListener到類

,並導入

進口com.wdullaer.materialdatetimepicker.date.DatePickerDialog;而不是'import android.app.DatePickerDialog;'

並添加此方法

private void showDatePicker() { 

    Calendar calendar = Calendar.getInstance(); 

    DatePickerDialog dpd = DatePickerDialog.newInstance(
      this, 
      calendar.get(Calendar.YEAR), 
      calendar.get(Calendar.MONTH), 
      calendar.get(Calendar.DAY_OF_MONTH) 
    ); 
    dpd.show(getActivity().getFragmentManager(), "DatePickerDialog"); 

    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); 
    String a = "07-03-2018"; // example 

    java.util.Date date = null; 

    try { 
     date = sdf.parse(a); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

    calendar = dateToCalendar(date); 
    System.out.println(calendar.getTime()); 

    List<Calendar> dates = new ArrayList<>(); 
    dates.add(calendar); 
    Calendar[] disabledDays1 = dates.toArray(new Calendar[dates.size()]); 
    dpd.setDisabledDays(disabledDays1); 

} 

    private Calendar dateToCalendar(Date date) { 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTime(date); 
     return calendar; 
    } 




@Override 
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) { 

    String date = dayOfMonth+"/"+(monthOfYear+1)+"/"+year; 
    textview.setText(date); 

} 

呼叫showDatePicker()。

多天

更改此

 String[] holidays = {"07-03-2018","05-03-2018","10-03-2018"}; 

    java.util.Date date = null; 

    for (int i = 0;i < holidays.length; i++) { 

     try { 
      date = sdf.parse(holidays[i]); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 

     calendar = dateToCalendar(date); 
     System.out.println(calendar.getTime()); 

     List<Calendar> dates = new ArrayList<>(); 
     dates.add(calendar); 
     Calendar[] disabledDays1 = dates.toArray(new Calendar[dates.size()]); 
     dpd.setDisabledDays(disabledDays1); 
    }