我确实有一个日期选择器,因为我只想让它只能选择今天的日期和未来日期,没有回复日期。需要一些指导如何完成,也许微调器可以做到这一点?
//start date
TableRow r8 = new TableRow(this);
TextView tvStartDate = new TextView(this);
tvStartDate.setText("Start Date: ");
final EditText etStartDate = new EditText(this);
etStartDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar dateNow = Calendar.getInstance();
int year = dateNow.get(Calendar.YEAR);
int month = dateNow.get(Calendar.MONTH);
int day = dateNow.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dp;
dp = new DatePickerDialog(AddPetActivity.this, new OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
// TODO Auto-generated method stub
selectedmonth = selectedmonth + 1;
etStartDate.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear);
}
}, year, month, day);
dp.setTitle("Select Date");
dp.show();
}
});
r8.addView(tvStartDate);
r8.addView(etStartDate);
tb.addView(r8);
答案 0 :(得分:3)
您可以访问对话框的日期选择器并设置它的最小/最大日期,如下所示:
dp.getDatePicker().setMinDate(dateNow.getTimeInMillis());
请注意,在为选择器设置最小日期时,它必须在选择器的选定日期之前,否则您将获得IllegalArgumentException
(当然最长日期相同)。