Android 显示大于开始日期的日期选择器(结束日期)?

Android show the datepicker(end date) greater than the start date?

大家好,我想在开始日期 select 之后显示日期选择器(结束日期) date.The 结束日期选择器将在开始 date.End 日期之后显示日期在开始日期之后和之前的显示日期选择器将不会显示。我要做什么? 我正在尝试的是

       fromdate = (EditText) findViewById(fromDate);
       todate = (EditText) findViewById(R.id.todate);
       fromdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showTruitonDatePickerDialog(view);
        }
    });
    todate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showToDatePickerDialog(view);
        }
    });
    }
public void showTruitonDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
   }
 public void showToDatePickerDialog(View v) {
    DialogFragment newFragment = new ToDatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}
 public static class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user

        fromdate.setText(day + "/" + (month + 1) + "/" + year);
           }   }

public static class ToDatePickerFragment extends DialogFragment implements
       DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }
   public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
        view.setMinDate(fromDate);
        todate.setText(day + "/" + (month + 1) + "/" + year); } }

尝试使用此代码

    public static boolean isDateAfter(String startDate,String endDate)
    {
    try
    {
        String myFormatString = "yyyy-M-dd"; // for example
        SimpleDateFormat df = new SimpleDateFormat(myFormatString);
        Date date1 = df.parse(endDate));
        Date startingDate = df.parse(startDate);

        if (date1.after(startingDate))
            return true;
        else
            return false;
    }
    catch (Exception e) 
    {

        return false;
    }
}

如果结束日期晚于开始日期,则return为真。

像这样定义 startDateCalendar

Calendar startDateCalendar=Callendar.getInstance();

并更新片段代码

public static class DatePickerFragment extends DialogFragment implements
            DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);
            DatePickerDialog datePickerDialog = new DatePickerDialog(mActivity,this, year, month,day);
            datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
            // Create a new instance of DatePickerDialog and return it
            return datePickerDialog;
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            // Do something with the date chosen by the user

            fromdate.setText(day + "/" + (month + 1) + "/" + year);
            startDateCalendar.set(Calendar.YEAR, year);
            startDateCalendar.set(Calendar.MONTH, month);
            startDateCalendar.set(Calendar.DAY_OF_MONTH, day);
        }   }

    public static class ToDatePickerFragment extends DialogFragment implements
            DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),this, startDateCalendar.get(Calendar.YEAR), startDateCalendar.get(Calendar.MONTH), startDateCalendar.get(Calendar.DAY_OF_MONTH));
            datePickerDialog.getDatePicker().setMinDate(startDateCalendar.getTimeInMillis());
            // Create a new instance of DatePickerDialog and return it
            return datePickerDialog;
        }
        public void onDateSet(DatePicker view, int year, int month, int day) {
            // Do something with the date chosen by the user
            view.setMinDate(fromDate);
            todate.setText(day + "/" + (month + 1) + "/" + year); } }

您可以对日期选择器对话框使用 setMinDate 和 setMaxDate 方法。

例如: DatePicker toPicker = Util.getDatePicker(getActivity(), toDateSetListener, toYear, toMonth, toDay) .getDatePicker(); toPicker.setMinDate(getToMinDate()); toPicker.setMaxDate(System.currentTimeMillis());

因此,当您关闭 fromDate 对话框时,只需将变量存储在 class 级别。

从年 从月 从日

然后当您启动 toDate(fromYear, fromMonth, fromDay) 到对话框时 然后代替日历。获取当前日期,只需使用 from 并将 +1 添加到日期以在第二天启动选择器。有道理吗?

有一个名为 MaterialDateRangePicker 的库可以完全解决您的问题 请找到 link https://github.com/borax12/MaterialDateRangePicker

     fromdate = (EditText) findViewById(fromDate);
    todate = (EditText) findViewById(R.id.todate);
    fromdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showTruitonDatePickerDialog(view);
        }
    });
    todate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showToDatePickerDialog(view);
        }
    });
}
public void showTruitonDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}

public void showToDatePickerDialog(View v) {
    DialogFragment newFragment = new ToDatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}

public static class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog datePickerDialog;
        datePickerDialog = new DatePickerDialog(getActivity(),this, year, 
        month,day);
        return datePickerDialog;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
        fromdate.setText(day + "/" + month  + "/" + year);
    }

}

public static class ToDatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {
   // Calendar startDateCalendar=Calendar.getInstance();
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
          String getfromdate = fromdate.getText().toString().trim();
          String getfrom[] = getfromdate.split("/");
        int year,month,day;
             year= Integer.parseInt(getfrom[2]);
             month = Integer.parseInt(getfrom[1]);
            day = Integer.parseInt(getfrom[0]);
        final Calendar c = Calendar.getInstance();
        c.set(year,month,day+1);
        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),this, year,month,day);
       datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
        return datePickerDialog;
    }
    public void onDateSet(DatePicker view, int year, int month, int day) {

        todate.setText(day + "/" + month  + "/" + year);
    }
}

我选择开始日期为....1/09/2017 然后我打开它显示的结束日期......2/09/2017