我正在使用日期选择器对话框来选择用户的出生日期,因此我可以显示该对话框。但不应允许用户比当前日期增加年份或日期或月份。我怎样才能做到这一点?
这是我的代码:
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button pickDate = (Button) findViewById(R.id.but);
pickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
}
}
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
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);
}
};
谢谢你。