问题
I'm using MaterialDateTimePicker. I want to disable particular weekday (like Sundays) of every month. Its mentioned in this post that we can do that using this library but how to disable particular weekdays?
I can certainly limit the number of months using the short code available but I don't want to limit number of months just disable weekdays from selection.
public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {
private TextView dateTextView;
private CheckBox modeDarkDate;
private CheckBox vibrateDate;
private CheckBox dismissDate;
private CheckBox limitDates;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find our View instances
dateTextView = (TextView)findViewById(R.id.date_textview);
Button dateButton = (Button)findViewById(R.id.date_button);
modeDarkDate = (CheckBox)findViewById(R.id.mode_dark_date);
vibrateDate = (CheckBox) findViewById(R.id.vibrate_date);
dismissDate = (CheckBox) findViewById(R.id.dismiss_date);
limitDates = (CheckBox) findViewById(R.id.limit_dates);
// check if picker mode is specified in Style.xml
modeDarkDate.setChecked(Utils.isDarkTheme(this, modeDarkDate.isChecked()));
// Show a datepicker when the dateButton is clicked
dateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
MainActivity.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.setThemeDark(modeDarkDate.isChecked());
dpd.vibrate(vibrateDate.isChecked());
dpd.dismissOnPause(dismissDate.isChecked());
if (limitDates.isChecked()) {
/* Calendar[] dates = new Calendar[13];
for(int i = -6; i <= 6; i++) {
Calendar date = Calendar.getInstance();
date.add(Calendar.MONTH, i);
dates[i+6] = date;
} */
// Add code here to disbale weekdays like Sundays.
// dpd.setSelectableDays(dates);
}
dpd.show(getFragmentManager(), "Datepickerdialog");
}
});
}
@Override
public void onResume() {
super.onResume();
DatePickerDialog dpd = (DatePickerDialog) getFragmentManager().findFragmentByTag("Datepickerdialog");
if(dpd != null) dpd.setOnDateSetListener(this);
}
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
String date = "You picked the following date: "+dayOfMonth+"/"+(++monthOfYear)+"/"+year;
dateTextView.setText(date);
}
}
回答1:
it is maybe a bit to late for this answer but I've got a solution that may help others. Please, try the following:
// Calendar saturday;
Calendar sunday;
List<Calendar> weekends = new ArrayList<>();
int weeks = 5;
for (int i = 0; i < (weeks * 7) ; i = i + 7) {
sunday = Calendar.getInstance();
sunday.add(Calendar.DAY_OF_YEAR, (Calendar.SUNDAY - sunday.get(Calendar.DAY_OF_WEEK) + 7 + i));
// saturday = Calendar.getInstance();
// saturday.add(Calendar.DAY_OF_YEAR, (Calendar.SATURDAY - saturday.get(Calendar.DAY_OF_WEEK) + i));
// weekends.add(saturday);
weekends.add(sunday);
}
Calendar[] disabledDays = weekends.toArray(new Calendar[weekends.size()]);
dpd.setDisabledDays(disabledDays);
This code disables the next 5 Sundays, if you wish to do it for a longer period, just need to modify week. If you wish to disable Saturdays too, just uncomment those lines.
If you want to do it for previous 5 Sundays then, just modify the for loop to:
for (int i = 0; i < (weeks * 7); i = i + 7) {
for(int j =0; j > (weeks*7) ; j = j - 7);
sunday = Calendar.getInstance();
sunday.add(Calendar.DAY_OF_YEAR, (Calendar.SUNDAY - sunday.get(Calendar.DAY_OF_WEEK) + 7 + i));
// saturday = Calendar.getInstance();
// saturday.add(Calendar.DAY_OF_YEAR, (Calendar.SATURDAY - saturday.get(Calendar.DAY_OF_WEEK) + i));
// weekends.add(saturday);
weekends.add(sunday);
}
so this second for loop will be used to disable previous 5 Sundays.
回答2:
Based on Xavi Del Palacio answer I created more elastic method
In disabled days you add Calendar days like disabledDays.add(Calendar.MONDAY)
Sorry about using kotlin instead of java, just wanted to give an idea how to rework this method to be easier in use
private fun disableDatesFor4Weeks(disabledDays: ArrayList<Int>) {
val disabledDaysInWeeks = ArrayList<Calendar>()
disabledDays.forEach {
dayInt ->
var day: Calendar
val weeks = 4
var week = 0
var i = 0
while (week < weeks * 7) {
day = Calendar.getInstance()
day.add(Calendar.DAY_OF_YEAR, dayInt - day.get(Calendar.DAY_OF_WEEK) + 7 * i)
disabledDaysInWeeks.add(day)
i += 1
week += 7
}
}
if (disabledDaysInWeeks.size > 0) {
val disabledDaysInWeeksTypedArray = disabledDaysInWeeks.toTypedArray()
datePickerDialog!!.maxDate = disabledDaysInWeeksTypedArray.last()
datePickerDialog!!.disabledDays = disabledDaysInWeeksTypedArray
}
}
回答3:
It might be late for answer i tried this code its disabled my sundays
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);
来源:https://stackoverflow.com/questions/37248732/disable-sundays-of-every-month-in-materialdatetimepicker