【问题标题】:Android show the datepicker(end date) greater than the start date?Android显示日期选择器(结束日期)大于开始日期?
【发布时间】:2017-10-04 05:06:34
【问题描述】:

大家好,我想在从开始日期选择的日期之后显示日期选择器(结束日期)。结束日期选择器将在起始日期之后显示日期。结束日期将在起始日期之后和之前显示日期选择器日期不会显示。我该怎么办? 我正在尝试的是

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

【问题讨论】:

  • 您是在问如何自动从一个日期选择器转到下一个日期选择器。我对你试图问的问题感到困惑,你能澄清一下吗
  • 使用DatePicker和setMinDate(long)和setMaxDate(long)或者你可以使用DatePickerDialog传递开始数据和(OnDateSetListener)[developer.android.com/reference/android/app/…
  • @Sam 我有两个编辑文本框。一个是开始日期和结束日期。当我单击每个编辑文本框(如开始日期)时,打开日期选择器以选择一个日期。现在我的问题结束日期( datepicker) 将显示起始日期之后的日期。
  • 哦,所以您想将结束日期的日期选择器更新为所选日期之后的第一个日期,对吗?如果是这样,只需在 fromDate 的类变量附近共享所选日期并在启动时传递给 toDate 作为要使用的日期而不是获取当前日期,使用 toDate 并为其添加 1 天
  • 如果需要,我会发布答案供您详细说明

标签: android android-calendar android-datepicker


【解决方案1】:
     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

【讨论】:

  • 但月份显示为负一(对于 7 月返回 06)!!?
【解决方案2】:

尝试使用此代码

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

如果 enddate 晚于开始日期,则返回 true。

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 它可以工作,但问题是结束日期选择器将针对当前日期工作。我想针对起始日期工作
    • @karthi 您正在从日历实例中获取年、月和日,因为显示当前日期日历实例返回当前日期
    • @J Ramesh 如果我选择开始日期 2016 年 5 月 11 日...然后结束日期将显示从开始日期的第二天。就像日期选择器将显示从 2016 年 6 月 11 日开始的日期。我不'开始日期不需要任何限制。
    • @karthi 然后删除 datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());这一行
    • @karthi 您想选择当前日期之前的开始日期吗?然后删除 setMinDate() 它在我这边工作
    【解决方案4】:

    您可以对 datepickerdialog 使用 setMinDate 和 setMaxDate 方法。

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

    【讨论】:

      【解决方案5】:

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

      从年份 从月 从天

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

      【讨论】:

        【解决方案6】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-03-02
          • 1970-01-01
          • 2012-08-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-06
          • 2017-02-08
          相关资源
          最近更新 更多