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

时间:2017-08-16 02:36:08

标签: android-studio datepickerdialog

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

离。用户想要输入支票/支票日期,我只允许过去的日期到当年。

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

1 个答案:

答案 0 :(得分:0)

您必须得到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();
}