已禁用特定日期范围

时间:2017-02-24 07:20:55

标签: android android-datepicker

我使用Betterpickers作为日历日期选择器。我需要禁用某些日期(来自API)。我试图禁用某些日期,但我的代码没有禁用这些日期。唯一禁用的日期是那些超出设定日期范围的日期,即minDate和maxDate。这些问题与我的代码有关吗?



public class MainActivity extends AppCompatActivity implements CalendarDatePickerDialogFragment.OnDateSetListener{

   Button dateBtn;
   private static final String FRAG_TAG_DATE_PICKER = "fragment_date_picker_name";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dateBtn = (Button)findViewById(R.id.dateBtn);
        dateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // For Calendar range
                DateTime now = DateTime.now();


                MonthAdapter.CalendarDay minDate = new MonthAdapter.CalendarDay(now.getYear(), now.getMonthOfYear()-1, now.getDayOfMonth());
                MonthAdapter.CalendarDay maxDate = new MonthAdapter.CalendarDay(now.getYear()+1, now.getMonthOfYear(), now.getDayOfMonth());

                Calendar startCal = Calendar.getInstance();
                startCal.setTimeInMillis(minDate.getDateInMillis());
                Calendar endCal = Calendar.getInstance();
                endCal.setTimeInMillis(maxDate.getDateInMillis());


                SparseArray<MonthAdapter.CalendarDay> disabledDays = new SparseArray<>();

                // For diable date range
                String startdateTime = "2017/03/12";
                String enddateTime = "2017/03/30";
                DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy/MM/dd");

                DateTime startJodaDate = dtf.parseDateTime(startdateTime);
                DateTime endJodaDate = dtf.parseDateTime(enddateTime);

                MonthAdapter.CalendarDay minDisableDate = new MonthAdapter.CalendarDay(startJodaDate.getYear(), startJodaDate.getMonthOfYear(), startJodaDate.getDayOfMonth());
                MonthAdapter.CalendarDay maxDisableDate = new MonthAdapter.CalendarDay(endJodaDate.getYear(), endJodaDate.getMonthOfYear(), endJodaDate.getDayOfMonth());

                Calendar startDisableCal = Calendar.getInstance();
                startDisableCal.setTimeInMillis(minDisableDate.getDateInMillis());

                Calendar endDisableCal = Calendar.getInstance();
                startDisableCal.setTimeInMillis(maxDisableDate.getDateInMillis());

                while (startDisableCal.before(endDisableCal)) {

                    int key = Utils.formatDisabledDayForKey(startDisableCal.get(Calendar.YEAR),
                            startDisableCal.get(Calendar.MONTH), startDisableCal.get(Calendar.DAY_OF_MONTH));
                    disabledDays.put(key, new MonthAdapter.CalendarDay(startDisableCal));

                    startDisableCal.add(Calendar.DATE, 1);

                }





                CalendarDatePickerDialogFragment cdp = new CalendarDatePickerDialogFragment()
                        .setDateRange(minDate, maxDate)
                        // Set Disabled Days
                        .setDisabledDays(disabledDays)
                        .setOnDateSetListener(MainActivity.this);

                cdp.show(getSupportFragmentManager(), FRAG_TAG_DATE_PICKER);
            }
        });

    }


    @Override
    public void onDateSet(CalendarDatePickerDialogFragment dialog, int year, int monthOfYear, int dayOfMonth) {
        int correctMonth = monthOfYear+1;
        System.out.println("sammy_DATE: "+dayOfMonth+"/"+correctMonth+"/"+year);
    }
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

我弄明白了。我只需要在CalendarDay中给出正确的月份。由于BetterPickers中的月份从0(零)开始,我们通常从1(一)开始,我只需要从minDisableDate和`maxDisableDate中减去1。

MonthAdapter.CalendarDay minDisableDate = new MonthAdapter.CalendarDay(startJodaDate.getYear(), startJodaDate.getMonthOfYear()-1, startJodaDate.getDayOfMonth());
MonthAdapter.CalendarDay maxDisableDate = new MonthAdapter.CalendarDay(endJodaDate.getYear(), endJodaDate.getMonthOfYear()-1, endJodaDate.getDayOfMonth());