Android 如何在 DatePickerDialog 中设置当前年份?

How to set current year in DatePickerDialog in Android?

当我的 DatePickerDialog 显示年份设置为“1900”时。我只知道如何限制过去的日期,但我不知道如何限制过去的年份。我想要发生的事情如下。

例如。用户要输入 Cheque/Check 日期,我只允许过去的日期到今年。

public void ChequeDate(View view) {
    DatePickerDialog dpd = new DatePickerDialog(PaymentHeader.this,
            new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {
                    if (year < mYear)
                        view.updateDate(mYear, mMonth, mDay);

                    if (monthOfYear < mMonth && year == mYear)
                        view.updateDate(mYear, mMonth, mDay);

                    if (dayOfMonth < mDay && year == mYear && monthOfYear == mMonth)
                        view.updateDate(mYear, mMonth, mDay);

                    if (monthOfYear < 9) {
                        if (dayOfMonth < 10) {
                            ChequeDateET.setText(year + "-0"
                                    + (monthOfYear + 1) + "-0" + dayOfMonth);
                        } else {
                            ChequeDateET.setText(year + "-0"
                                    + (monthOfYear + 1) + "-" + dayOfMonth);
                        }
                    } else {
                        if (dayOfMonth < 10) {
                            ChequeDateET.setText(year + "-"
                                    + (monthOfYear + 1) + "-0" + dayOfMonth);
                        } else {
                            ChequeDateET.setText(year + "-"
                                    + (monthOfYear + 1) + "-" + dayOfMonth);
                        }
                    }
                }
            }, mYear, mMonth, mDay);
    //dpd.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
    dpd.show();
}

您必须获取 current calendar instance 并且由此您必须获取 year, month and date 对象以作为 DatePickerDialog 方法中的参数传递并启用 setMinDate 方法。 现在检查以下代码

public void ChequeDate(View view) {
    Calendar calendar = Calendar.getInstance();
    int date = calendar.get(Calendar.DATE);
    int month = calendar.get(Calendar.MONTH);
    int year = calendar.get(Calendar.YEAR);
    DatePickerDialog dpd = new DatePickerDialog(PaymentHeader.this,
            new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {
                    if (year < mYear)
                        view.updateDate(mYear, mMonth, mDay);

                    if (monthOfYear < mMonth && year == mYear)
                        view.updateDate(mYear, mMonth, mDay);

                    if (dayOfMonth < mDay && year == mYear && monthOfYear == mMonth)
                        view.updateDate(mYear, mMonth, mDay);

                    if (monthOfYear < 9) {
                        if (dayOfMonth < 10) {
                            ChequeDateET.setText(year + "-0"
                                    + (monthOfYear + 1) + "-0" + dayOfMonth);
                        } else {
                            ChequeDateET.setText(year + "-0"
                                    + (monthOfYear + 1) + "-" + dayOfMonth);
                        }
                    } else {
                        if (dayOfMonth < 10) {
                            ChequeDateET.setText(year + "-"
                                    + (monthOfYear + 1) + "-0" + dayOfMonth);
                        } else {
                            ChequeDateET.setText(year + "-"
                                    + (monthOfYear + 1) + "-" + dayOfMonth);
                        }
                    }
                }
            }, year, month, date);
    dpd.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
    dpd.show();
}