我使用此链接Date Picker
创建了我的日期选择器我知道我们可以使用setMinDate()方法来设置最短日期,但如果我们按照链接中提供的方式实现日期选择器,我就不明白该怎么做。 使用这种方法时,我没有找到任何方法。 日期选择代码
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
tv_date= (TextView)getActivity().findViewById(R.id.tv_date);
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);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
tv_date.setText(new StringBuilder().append(day).append("-")
.append(month+1).append("-").append(year));
date=tv_date.getText().toString();
}
}
答案 0 :(得分:1)
正如您在此链接中所看到的:DatePicker#setMinDate,我们需要获取日期选择器对象以在选择器对话框中设置最小或最大日期。
请参阅下面给出的代码段,它将解决您的问题:
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@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
DatePickerDialog dialog = new DatePickerDialog(context, this, year, month, day);
//set min date (ex. current date and time) - pass your custom date and time in milliseconds
dialog.getDatePicker().setMinDate(c.getTimeInMillis());
//set max date
//dialog.getDatePicker().setMaxDate(maxDate);
return dialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
}
希望它会对你有所帮助。
答案 1 :(得分:1)
使用此代码:
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);
DatePickerDialog d = new DatePickerDialog(getActivity(), listener, year, month, day);
DatePicker dp = d.getDatePicker();
dp.setMinDate(c.getTimeInMillis());
return d;