不要在 DatePicker 中接受过去的日期
Don't Accept Past dates in DatePicker
我不想在 DatePicker 中接受过去的日期。这是我的代码,我用它来显示日期选择器并在 EditText 中设置日期。
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
public EditText editDate;
private Calendar dateTime = Calendar.getInstance();
private SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMMM yyyy");
public DatePickerFragment(EditText editText) {
editDate = editText;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
dateTime.set(year, monthOfYear, dayOfMonth);
editDate.setText(dateFormatter
.format(dateTime.getTime()));
}
}
为 DatePicker 设置最小日期
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the today date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
DatePickerDialog dp = new DatePickerDialog(getActivity(), this, year, month, day);
// Use the today date as the Min date in the picker
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
dp.getDatePicker().setMinDate(c.getTimeInMillis());
} else {
Log.w(TAG, "API Level < 11 so not restricting date range...");
}
return dp;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
}
我不想在 DatePicker 中接受过去的日期。这是我的代码,我用它来显示日期选择器并在 EditText 中设置日期。
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
public EditText editDate;
private Calendar dateTime = Calendar.getInstance();
private SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMMM yyyy");
public DatePickerFragment(EditText editText) {
editDate = editText;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
dateTime.set(year, monthOfYear, dayOfMonth);
editDate.setText(dateFormatter
.format(dateTime.getTime()));
}
}
为 DatePicker 设置最小日期
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the today date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
DatePickerDialog dp = new DatePickerDialog(getActivity(), this, year, month, day);
// Use the today date as the Min date in the picker
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
dp.getDatePicker().setMinDate(c.getTimeInMillis());
} else {
Log.w(TAG, "API Level < 11 so not restricting date range...");
}
return dp;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
}