我正在处理表单以输入用户信息。有EditText
来设定出生日期。点击它会打开DatePickerDialogue
。我希望将日期限制设置为当前日期,以便用户无法从将来选择日期。
这是我的代码:
DatePickerDialog.OnDateSetListener dpd;
Edittext etDOB = (EditText) findViewById(R.id.etDate);
etDOB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new DatePickerDialog(ActivityCustomerInfo.this,dpd,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
dpd=new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String month = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
etDOB.setText(new StringBuilder().append((month)+" ").append((dayOfMonth)+", ").append(year));
}
};
我知道函数setMaxDate()但无法理解如何将它应用于我的代码。
I followed the answers here but the code doesn't seem to match with mine.