将日期上限和下限设置为日期选择器对话框

时间:2013-04-16 05:23:49

标签: java android datepicker limit

我创建了一个活动,其中有一个用于插入用户出生日期的字段。我使用了日期选择器来实现这一目标。

这是我正在做的源代码:

// onclick dob edittext open date picker dialog
        dob.setFocusableInTouchMode(false);
        dob.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                //open date picker
                  showDialog(DATE_DIALOG_ID);

            }
        }); 


//======================For picking birth day==========================
    // Creating dialog
        @Override
        protected Dialog onCreateDialog(int id) {
            Calendar c = Calendar.getInstance();
            int cyear = c.get(Calendar.YEAR);
            int cmonth = c.get(Calendar.MONTH);
            int cday = c.get(Calendar.DAY_OF_MONTH);


            switch (id) {
                case DATE_DIALOG_ID:
                    return new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);
            }
            return null;
        }
        private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
            // onDateSet method
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//              String date_selected = String.valueOf(monthOfYear+1)+" /"
//                                    +String.valueOf(dayOfMonth)+" /"
//                                    +String.valueOf(year);

                String date_selected = String.valueOf(dayOfMonth)+" /"
                        +String.valueOf(monthOfYear+1)+" /"
                        +String.valueOf(year);
                //Toast.makeText(RegisterActivity.this, "Selected Date is ="+date_selected, Toast.LENGTH_SHORT).show();
                dob.setText(date_selected);
            }
        };
//======================For picking birth day==========================

现在我要做的是,将日期选择器限制为特定年份。 像这样:

int maxYear = c.get(Calendar.YEAR) - 15; // this year ( 2013 ) - 15 = 1998
                int maxMonth = c.get(Calendar.MONTH);
                int maxDay = c.get(Calendar.DAY_OF_MONTH);

                int minYear = maxYear-49;
                int minMonth = 0; // january
                int minDay = 25; 

如何将上述界限设置为日期选择器?

// ----------------编辑------------------------- -

我做过这样的事情:

//======================For picking birth day==========================
    // Creating dialog
        @Override
        protected Dialog onCreateDialog(int id) {
            Calendar c = Calendar.getInstance();
            maxYear = c.get(Calendar.YEAR) - 15; // this year ( 2013 ) - 15 = 1998
            maxMonth = c.get(Calendar.MONTH);
            maxDay = c.get(Calendar.DAY_OF_MONTH);




            switch (id) {
                case DATE_DIALOG_ID:
                    return new DatePickerDialog(this, mDateSetListener, maxYear, maxMonth, maxDay);
            }
            return null;
        }


        DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {



                int minYear = maxYear-49;
                int minMonth = 0; // january
                int minDay = 25;

            public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth) {

                Date current = new Date(year, monthOfYear, dayOfMonth);

                Date minDate = new Date(minYear, minMonth, minDay);
                Date maxDate = new Date(maxYear, maxMonth, maxDay);
                if( current.getTime() < minDate.getTime() )
                {
                    //set to minimum date
                }else if( current.getTime() > maxDate.getTime() )
                {
                    //Set to max Date
                }

            }

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                // TODO Auto-generated method stub
                 String date_selected = String.valueOf(dayOfMonth)+" /"
                            +String.valueOf(monthOfYear+1)+" /"
                            +String.valueOf(year);
                    //Toast.makeText(RegisterActivity.this, "Selected Date is ="+date_selected, Toast.LENGTH_SHORT).show();
                    dob.setText(date_selected);

            }
        };
//======================For picking birth day==========================

这是修正上限,但是下限或minDate没有得到修复!该怎么办?

1 个答案:

答案 0 :(得分:2)

设置OnDateChangedListener()对于您DatePickeronDateChanged内部功能,如果日期设置大于决定限制,那么DatePicker.updateDate(minYear, minMonth, minDay);使用此方法重置它。

Date minDate = new Date(1990, 10, 25);
Date maxDate = new Date(2013, 4, 15);



OnDateChangedListener listener = new OnDateChangedListener() {

    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {

        Date current = new Date(year, monthOfYear, dayOfMonth);

        if( current.getTime() < minDate.getTime() )
        {
            //set to minimum date
        }else if( current.getTime() > maxDate.getTime() )
        {
            //Set to max Date
        }

    }
};
YourDatePicker.init(2013, 4, 15, listener);