Is it possible to disable certain weekdays in Date

2019-02-26 06:51发布

I need a DatePicker for a subscription service where the customer can select view dates. But not every day is available. For example on Wednesdays is no delivery.

Is it possible to disable specific week days?

Or do I have to make an alert afterwards to tell him this is not possible.

1条回答
够拽才男人
2楼-- · 2019-02-26 07:47

in this project you will find the logic you need. DatePicker without sundays

Relevant snippet:

 if (choosenDate.get(Calendar.DAY_OF_WEEK) == 
                    Calendar.SUNDAY || 
                    now.compareTo(choosenDate) < 0) {
                    dateTextView.setTextColor(
                        Color.parseColor("#ff0000")
                    );
                    ((Button) dialog.getButton(
                    AlertDialog.BUTTON_POSITIVE))
                        .setEnabled(false);
                } else {
                    dateTextView.setTextColor(
                        Color.parseColor("#000000")
                    );
                    ((Button) dialog.getButton(
                    AlertDialog.BUTTON_POSITIVE))
                        .setEnabled(true);
                }

You can rewrite this, of course, to block Wednesdays or any other days of the week. The code was written in the OnChangeListener of the Datepicker in the last java lines of the sample project.

查看更多
登录 后发表回答