Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
777 views
in Technique[技术] by (71.8m points)

java - Setting upper and lower date limits to date picker dialog

I have created an activity in which there is a field for inserting birth date of the user. I have used a date picker to achieve this.

This is the source code of what I am doing:

// 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==========================

Now what I want to do is, to limit the date picker to particular year. Like this:

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; 

What should I do to set the above mentioned bounding to the date picker?

//----------------Edit---------------------------

I have done something like this:

//======================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==========================

This is fixing the upper limit, but the lower limit or the minDate is not getting fixed! What is to be done?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Set OnDateChangedListener() For you DatePicker and inside onDateChanged function if the date set is greater than the decide limit if so DatePicker.updateDate(minYear, minMonth, minDay); use this method to reset it.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...