我想显示当前月份的日期选择器对话框而不是年份。我已经将setMin用于当前日期工作正常,但我仍然可以追溯到2015年1月,即使它不可选。我如何使它工作?
public void show() {
Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
Post.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.setMinDate(now.getInstance());
now.add(Calendar.DAY_OF_MONTH, 30);
dpd.setMaxDate(now);
dpd.show(getFragmentManager(), "Datepickerdialog");
}
答案 0 :(得分:0)
你走了,这就是工作。
DatePickerDialog dlg = new DatePickerDialog(context, datePickerListener,
dueDateCalendar.get(Calendar.YEAR),
dueDateCalendar.get(Calendar.MONTH),
dueDateCalendar.get(Calendar.DAY_OF_MONTH))
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
int year = getContext().getResources()
.getIdentifier("android:id/year", null, null);
if(year != 0){
View yearPicker = findViewById(year);
if(yearPicker != null){
yearPicker.setVisibility(View.GONE);
}
}
}
};
return dlg;
答案 1 :(得分:0)
请尝试以下操作,不会向您显示过去的日期。它会从当前日期开始显示。
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(this,
AlertDialog.THEME_HOLO_LIGHT, this, year, month, day);
dpd.show();
calendar.add(Calendar.DATE, 0);
// Set the Calendar new date as minimum date of date picker
dpd.getDatePicker().setMinDate(calendar.getTimeInMillis());
(or)
dpd.setMinDate(System.currentTimeMillis() - 1000);