简体   繁体   中英

DatepickerDialog. How to reopen it when invalid date set

Alright. so I made a datepicker dialog that opens on a TextView Click. this part works fine. my problem happens when i have 2 dates. if the second date is earlier than the first date, then the datepicker should re-open and ask for a new "valid" date. This is where i come into my problem. My code as it is now(below) tells me if the date is wrong, but does not re-open the datepicker to allow me to set a new date. How can I go about doing that? I tried making a new method and have the same code in it with a different datepickerdialog, but that overlaps 2 datepickers together and the date selected is always from the second one.

    public void onClick(final View v) {
    final Calendar date =Calendar.getInstance();
    int dateyear=date.get(Calendar.YEAR);
    int datemonth=date.get(Calendar.MONTH);
    int dateday=date.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog dp=new DatePickerDialog(AddClaim.this, new OnDateSetListener() {                  
        public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
            int month = selectedmonth;
            date.set(selectedyear, month, selectedday);
            Date myDate = date.getTime();

            String returnDate = finalDate.format(myDate);
            if (call){
                sdate_field.setText(returnDate);
                try {
                    sdate = finalDate.parse(returnDate);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }else{
                try {
                    if (finalDate.parse(returnDate).before(sdate)){
                        Toast.makeText(AddClaim.this, "End date cannot be before Start date", Toast.LENGTH_LONG).show(); //after this a new datepickerdialog should open....


                    }else{
                        edate_field.setText(returnDate);
                    }
                } catch (ParseException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                try {
                    edate = finalDate.parse(returnDate);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    },dateyear, datemonth, dateday);
    if (call){
        dp.setTitle("Select Start Date");                
    }else{
        dp.setTitle("Select End Date");
    }
        dp.show(); 
    }

Create a boolean field

boolean isValid;

Update it from the onDateSet method. Set it to true if the selected date is valid or else set it to false .

And check for the state of the boolean field and re-instantiate the picker if necessary.

Most importantly, I suggest you to create a new class for the picker.

Updating...

public static class AppDatePickerDialog extends DialogFragment
            implements DatePickerDialog.OnDateSetListener {

    private int mYear, mMonth, mDay;
    private String sMonth;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new DatePickerDialog(getActivity(), this, mYear, mMonth, mDay);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        //you logic to set the field to true or false
    }
}

So to open the picker you can do the following

AppDatePickerDialog datePicker = new AppDatePickerDialog();
datePicker.show(getSupportFragmentManager(), "YOUR_TAG");

You can put the above two lines inside the onDateSet method, if you want to reopen the picker as per the boolean field.

Edit

Inside the onDateSet method put the following:

if (!isValid) {
    AppDatePickerDialog datePicker = new AppDatePickerDialog();
    datePicker.show(getSupportFragmentManager(), "YOUR_TAG");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM