在MaterialDateTimePicker中禁用每个月的星期日

时间:2016-05-16 07:23:39

标签: android android-datepicker

我正在使用MaterialDateTimePicker。我想禁用每个月的特定工作日(如星期日)。它在post中提到我们可以使用这个库来做但how to disable particular weekdays?我当然可以使用短代码来限制月数,但我不想限制工作日的禁用月数来自选择。

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);
    }
}

3 个答案:

答案 0 :(得分:12)

对于这个答案来说可能有点迟到但是我有一个可以帮助别人的解决方案。请尝试以下方法:

<div class="ourservices">
<div class="cont kiri" tabindex="0">

<div class="kirig">
<img border="0" src="http://www.mo2properties.com/wp-content/uploads/2016/02/click-here-pointer-md.png" />
</div>

<div class="kirip">
<p>click read more click read more click read more</p>
<a href="http://stackoverflow.com">
<span class="more">
Read More
</span>
</a>
</div>

</div>
<div class="clearfix" />
</div>

此代码禁用接下来的5个星期日,如果您希望长时间执行此操作,只需要修改一周。如果您也希望禁用星期六,请取消注释这些行。

如果您想在之前的5个星期日进行,只需将for循环修改为:

    // 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);

所以这个第二个for循环将用于禁用之前的5个星期日。

答案 1 :(得分:1)

根据Xavi Del Palacio的回答,我创造了更有弹性的方法 在停用的日子里,您可以添加disabledDays.add(Calendar.MONDAY)

等日历日

很抱歉使用kotlin而不是java,只是想知道如何重做这种方法以便于使用

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
        }
    }

答案 2 :(得分:0)

  

这可能是迟到的回答我试过这个代码它的残疾我的星期天

            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);