Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法获取日期选择器,请选择当前日期,保持启用上一个日期_Android_Date_Datepicker - Fatal编程技术网

Android 无法获取日期选择器,请选择当前日期,保持启用上一个日期

Android 无法获取日期选择器,请选择当前日期,保持启用上一个日期,android,date,datepicker,Android,Date,Datepicker,我正在使用datepicker片段显示一个datepicker。我用以前选择的日期初始化日期选择器 在加载带有上一个日期的datepicker时,我无法从datepicker中选择当前日期。我只在API级别21中面对这个问题。它在所有API级别都能工作,API 21除外 每当我从datepicker中删除maxDate时,它就开始工作了。但在我的情况下,我需要禁用未来的日期 我不明白我的代码出了什么问题 这是我的密码 public class DatePickerFragment extends

我正在使用datepicker片段显示一个datepicker。我用以前选择的日期初始化日期选择器

在加载带有上一个日期的datepicker时,我无法从datepicker中选择当前日期。我只在API级别21中面对这个问题。它在所有API级别都能工作,API 21除外

每当我从datepicker中删除maxDate时,它就开始工作了。但在我的情况下,我需要禁用未来的日期

我不明白我的代码出了什么问题

这是我的密码

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    public DatePickerFragment() {
    }

    private String selectedDate = "";
    private Interfaces.OnDateSelectedClickListener onDateSelectedClickListener;

    public DatePickerFragment(Interfaces.OnDateSelectedClickListener onDateSelectedClickListener, String selectedDate) {
        this.onDateSelectedClickListener = onDateSelectedClickListener;
        this.selectedDate = selectedDate;

    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar calendar = Calendar.getInstance();
        int year = 0, month = 0, day = 0;

        if (StaticUtils.isTextEmpty(selectedDate)) {
            year = calendar.get(Calendar.YEAR);
            month = calendar.get(Calendar.MONTH);
            day = calendar.get(Calendar.DAY_OF_MONTH);
        } else {
            try {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.getDefault());
                Date date = simpleDateFormat.parse(selectedDate);
                calendar.setTimeInMillis(date.getTime());
                year = calendar.get(Calendar.YEAR);
                month = calendar.get(Calendar.MONTH);
                day = calendar.get(Calendar.DAY_OF_MONTH);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        DatePickerDialog datePicker;
        if (Build.VERSION.SDK_INT < 21) {
            datePicker = new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_LIGHT, this, year, month, day);
        } else {
            datePicker = new DatePickerDialog(getActivity(), R.style.datepicker, this, year, month, day);
        }

        datePicker.getDatePicker().setMaxDate(System.currentTimeMillis()); // disable future dates
        datePicker.setTitle("");
        datePicker.setCustomTitle(null);
        return datePicker;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        if (onDateSelectedClickListener != null) {
            onDateSelectedClickListener.onDateSelect(StaticUtils.getDateTimeFromCalendar(day, month, year));
        }
    }
}
更新:


在应用@Lalchand代码后,如果我已经用上一个日期加载了日历,但在API级别21中它仍然不起作用,那么我可以选择当前日期。有人能帮我吗?或者给我改进代码的建议

我认为下面的链接将为您提供答案:

因此,在用户选择日期后,如果用户选择了错误的日期,则向用户发出警告或警报消息

找到的解决方案:使用此链接中提到的库


下面的代码块将帮助您

通过所选日期=2017年4月3日


经过更多的研究,我知道系统将不允许我选择我的当前日期,因为我已经设置了最大日期。因此,允许我的用户选择我更改的当前日期

datePicker.getDatePicker.setMaxDateSystem.currentTimeMillis;到

datePicker.getDatePicker.setMaxDateSystem.currentTimeMillis+1000*60*60

通过应用上面的解决方法,我可以选择当前日期,也可以用以前的日期加载日历

datePicker.getDatePicker().setMaxDate((maxDate + 1).getTime() - 1)
我猜他们编写的代码如下:

if (getCurrentTimeInMillis() > getMaxDate().getTimeInMillis()) {
    // you can't select the future time
    return;
}
if (getCurrentDate() > getMaxDate()) {
    // you can't select the future time
    return;
}
修复错误后,应如下所示:

if (getCurrentTimeInMillis() > getMaxDate().getTimeInMillis()) {
    // you can't select the future time
    return;
}
if (getCurrentDate() > getMaxDate()) {
    // you can't select the future time
    return;
}

很明显,您可以通过将其设置为一天中的最大时间单位毫秒来修复它

让我再试一次。我将告诉您结果。它没有加载具有以前所选日期的日期选择器。与我得到的结果相同。仍然无法选择当前日期:嗨,它正在工作。谢谢你的帮助@LalchandI不明白这一点。你能简单地解释一下吗?意思是不要选择错误的日期,请选择从今天开始的日期。但我的客户希望看到他们处于禁用模式:你在onDateChanged中尝试过任何自定义逻辑吗?此问题的解决方案在此链接,而此代码可能会解决此问题,如何以及为什么解决这个问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。