final DatePickerDialog.OnDateSetListener lastdateof = new DatePickerDialog.OnDateSetListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
String format = "dd MMMM yy";
calendar.set(calendar.YEAR, year);
calendar.set(calendar.MONTH, monthOfYear);
calendar.set(calendar.DAY_OF_MONTH, dayOfMonth);
datePicker.setMinDate(System.currentTimeMillis()-1000);
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
lastdate.setText(sdf.format(calendar.getTime()));
}
};
lastdate.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View view) {
new DatePickerDialog(AddActivity.this, lastdateof, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
如何在当前日期之前禁用或不能选择数据选择器日期?
答案 0 :(得分:2)
在显示对话框
之前尝试使用setMinDate Calendar myCalendar = Calendar.getInstance();
lastdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Datepiker dialog
DatePickerDialog datePickerDialog = new DatePickerDialog(AddEditDiaryActivity.this, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
datePickerDialog.show();
}
});
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String myFormat = "yyyy-MM-dd"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.ENGLISH);
lastdate.setText(sdf.format(myCalendar.getTime()));
}
};
答案 1 :(得分:0)
您必须在显示日期选择器之前设置最小日期。
datePicker.setMinDate(System.currentTimeMillis()-1000);
在拨打日期选择器的show()
之前,请先调用上述代码。
例如:
datePicker.setMinDate(System.currentTimeMillis()-1000);
datePicker.show();