使用自定義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);
}
非常感謝,這個偉大的工程。我不知道爲什麼標準的android之一是如此有限。 – Zapnologica
如何通過上述庫中的日期禁用?假設如何禁用所有周末? – prateek31
創建不包含週末的日期數組,並將其作爲可選日期傳遞。 – himanshu1496