我正在使用日期选择器对话框来选择用户的出生日期,因此我可以显示该对话框。但不应允许用户增加年份,日期或月份而不是现在的日期。我怎么能这样做?
这是我的代码:
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);
}
};
谢谢。
答案 0 :(得分:5)
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
DatePickerDialog _date = new DatePickerDialog(this, mDateSetListener,
mYear, mMonth, mDay){
@Override
public void onDateChanged(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);
}
};
return _date;
}
}
您必须使用上面的oncreate对话框来限制将来的日期
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
//do your stuff when user clicks on set button in dialog
}
};
答案 1 :(得分:0)
1. Since API level 11 there is a method for that:
DatePicker.setMaxDate(long maxDate)
2. If it has to work in previous versions, use this method:
public void init(int year, int monthOfYear, int dayOfMonth, DatePicker.OnDateChangedListener onDateChangedListener)
您可以将自己的OnDateChangedListener“重置”为最新的有效日期:
DatePicker picker = ...
int year = ...
int monthOfYear = ...
int dayOfMonth = ...
picker.init(year, monthOfYear, dayOfMonth, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// check if current date is OK
boolean dateOk = ...
if (!dateOk) {
// correct the date, but be sure the corrected date is OK
// => otherwise you might get endless recursion
year = ...
monthOfYear = ...
dayOfMonth = ...
// update the date widget with the corrected date values
view.updateDate(year, monthOfYear, dayOfMonth);
}
}
});
答案 2 :(得分:0)
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.content.Context;
import android.widget.DatePicker;
public class MyDatePickerDialog extends DatePickerDialog {
private CharSequence title;
final Calendar c = Calendar.getInstance();
int maxYear = c.get(Calendar.YEAR) -100; // this year ( 2013 ) +100 =2113
int maxMonth = c.get(Calendar.MONTH);
int maxDay = c.get(Calendar.DAY_OF_MONTH);
int minYear = c.get(Calendar.YEAR);
int minMonth = c.get(Calendar.MONTH);; // january
int minDay = c.get(Calendar.DAY_OF_MONTH);
/**
* Instantiates a new my date picker dialog.
*
* @param context the context
* @param callBack the call back
* @param year the year
* @param monthOfYear the month of year
* @param dayOfMonth the day of month
*/
public MyDatePickerDialog(Context context, OnDateSetListener callBack,
int year, int monthOfYear, int dayOfMonth) {
super(context, callBack, year, monthOfYear, dayOfMonth);
}
/**
* Sets the permanent title.
*
* @param title the new permanent title
*/
public void setPermanentTitle(CharSequence title) {
this.title = title;
setTitle(title);
}
@Override
public void onDateChanged(DatePicker view, int year, int month, int day) {
//super.onDateChanged(view, year, month, day);
disable(view, year, month, day);
setTitle(title);
}
private void disable(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
if (year < maxYear)
view.updateDate(maxYear, maxMonth, maxDay);
if (monthOfYear < maxMonth && year == maxYear)
view.updateDate(maxYear, maxMonth, maxDay);
if (dayOfMonth < maxDay && year == maxYear && monthOfYear == maxMonth)
view.updateDate(maxYear, maxMonth, maxDay);
if (year >minYear)
// view.updateDate(minYear, minDay, minMonth);
view.updateDate(minYear, monthOfYear, dayOfMonth);
if (monthOfYear > minMonth && year == minYear)
// view.updateDate(minYear, minMonth, minMonth);
view.updateDate(minYear, minMonth, dayOfMonth);
if (dayOfMonth > minDay && year == minYear && monthOfYear == minMonth)
view.updateDate(minYear, minMonth, minDay);
}
}