禁用星期日和任何其他特定日期

时间:2016-11-25 07:40:05

标签: java android-datepicker

我希望在android日期选择器中的特定月份禁用星期日和特定日期

我使用的Datepicker是android datepicker

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 1);
calendar.setMinimalDaysInFirstWeek(27);
// calendar.add(Calendar.DAY_OF_MONTH, 1);
//calendar.set(2016,11,23);
// new   DatePickerDialog(LoginSuccess.this, listener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
DatePickerDialog dpDialog = new DatePickerDialog(LoginSuccess.this, listener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
// dpDialog.updateDate(2016,11,23);

DatePicker datePicker = dpDialog.getDatePicker();
//datePicker.updateDate(2016,11,23);
// Date newDate = calendar.getTime();
//dpDialog.getDatePicker().setMinDate(newDate.getTime());
//datePicker.init();

if (calendar.get(Calendar.DAY_OF_MONTH)==Calendar.SUNDAY) {
   // dpDialog.setTitle("Sorry Sundays wont be accepted");
   // dpDialog.setMessage("not selected");
   // dpDialog.show();
    //datePicker.setBackgroundColor(000000);
    Color.parseColor("#000000");
}
else {
    long now = System.currentTimeMillis();

    datePicker.setFirstDayOfWeek(Calendar.MONDAY);
    datePicker.setMinDate(now + (1000 * 60 * 60 * 24 * 1));
    datePicker.setMaxDate(calendar.getTimeInMillis());
    dpDialog.show();
}

public DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);

        //calendar.set(2016,11,23);
        display.setText("Selected Date:" + (dayOfMonth) + "/" + (monthOfYear + 1) + "/" + year);
            Button ok = (Button) findViewById(R.id.confirm);
            ok.setOnClickListener((v) -> {
                Intent intent = new Intent(LoginSuccess.this, Confirm.class);
                intent.putExtra("Date", dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
                intent.putExtra("name", (String) spinnerOsversions.getSelectedItem());
                intent.putExtra("email4", email3.toString());
                intent.putExtra("bikeno4", bikeno3.toString());
                intent.putExtra("fname", fname.toString());
                //intent.putExtras(b);
                startActivity(intent);
                finish();
            });
    }

SetMinDatesetMaxdate工作正常,但我只想禁用星期日和特定日子。

3 个答案:

答案 0 :(得分:3)

您可以使用https://github.com/wdullaer/MaterialDateTimePicker#additional-options

使用以下代码:
datePicker.setSelectableDays(Calendar[] days)

并将所有可用日期放入datePicker。

答案 1 :(得分:3)

使用默认的DatePickerDialog无法做到这一点。您可能需要查看MaterialDateTimePicker

com.wdullaer.materialdatetimepicker.date.DatePickerDialog中,你有方法public void setSelectableDays(Calendar[] selectableDays)

答案 2 :(得分:2)

您正在使用calendar.get(Calendar.DAY_OF_MONTH)来匹配星期日 使用DAY_OF_WEEK,其值为SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY和SATURDAY

if (calendar.get(Calendar.DAY_OF_WEEK).equals(Calendar.SUNDAY)) {
   // dpDialog.setTitle("Sorry Sundays wont be accepted");
   // dpDialog.setMessage("not selected");
   // dpDialog.show();
    //datePicker.setBackgroundColor(000000);
    Color.parseColor("#000000");
}