在 DatePickerDialog 中禁用过去的日期
Disable past date in DatePickerDialog
我想添加代码 .getDatePicker().setMinDate(newDate.getTime() - (newDate.getTime()%(24*60*60*1000)))
以禁用过去的日期,但突然 .show()
无法使用。
有人可以教我如何解决这类问题吗?
代码:
btnDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectDate();
}
});
private void selectDate()
{
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String selectedDate = dateFormat.format(calendar.getTime());
btnDate.setText(selectedDate);
btnDate.setTextColor(getResources().getColor(colorPrimaryDark));
}
};
Date newDate = calendar.getTime();
new DatePickerDialog(TutorAddNewTimeSlotActivity.this, dateSetListener,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
}
你可以简单地这样做
datePickerDialog.datePicker.minDate=Date().time
您可以创建自己的日期选择器class,方法是继承默认选择器并像下面的代码那样定义最短日期
class DatePicker(var onDateSetListener: DatePickerDialog.OnDateSetListener): DialogFragment() {
var date: Date? = null
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
val datePickerDialog= DatePickerDialog(activity!!,onDateSetListener, year, month, day)
datePickerDialog.datePicker.minDate=Date().time
return datePickerDialog
}
}
您可以像这样转换您的方法以设置最短日期
private void selectDate()
{
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String selectedDate = dateFormat.format(calendar.getTime());
btnDate.setText(selectedDate);
btnDate.setTextColor(getResources().getColor(colorPrimaryDark));
}
};
Date newDate = calendar.getTime();
DatePickerDialog dialog= new DatePickerDialog(this, dateSetListener,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
dialog.getDatePicker().setMinDate(Calendar.getInstance().getTimeInMillis());
dialog.show();
}
首先声明如下:
private Calendar myCalendar = Calendar.getInstance();
然后喜欢你的代码:-
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String selectedDate = dateFormat.format(calendar.getTime());
btnDate.setText(selectedDate);
btnDate.setTextColor(getResources().getColor(colorPrimaryDark));
}
};
在您的代码而不是最后两行中进行此更改:
`Date newDate = calendar.getTime();
new DatePickerDialog(TutorAddNewTimeSlotActivity.this, dateSetListener,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();`
改变它,使它像下面这样:
datePickerDialog.getDatePicker().setMinDate(myCalendar.getTimeInMillis());
datePickerDialog.show();
这将禁用过去的日期。
我想添加代码 .getDatePicker().setMinDate(newDate.getTime() - (newDate.getTime()%(24*60*60*1000)))
以禁用过去的日期,但突然 .show()
无法使用。
有人可以教我如何解决这类问题吗?
代码:
btnDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectDate();
}
});
private void selectDate()
{
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String selectedDate = dateFormat.format(calendar.getTime());
btnDate.setText(selectedDate);
btnDate.setTextColor(getResources().getColor(colorPrimaryDark));
}
};
Date newDate = calendar.getTime();
new DatePickerDialog(TutorAddNewTimeSlotActivity.this, dateSetListener,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
}
你可以简单地这样做
datePickerDialog.datePicker.minDate=Date().time
您可以创建自己的日期选择器class,方法是继承默认选择器并像下面的代码那样定义最短日期
class DatePicker(var onDateSetListener: DatePickerDialog.OnDateSetListener): DialogFragment() {
var date: Date? = null
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
val datePickerDialog= DatePickerDialog(activity!!,onDateSetListener, year, month, day)
datePickerDialog.datePicker.minDate=Date().time
return datePickerDialog
}
}
您可以像这样转换您的方法以设置最短日期
private void selectDate()
{
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String selectedDate = dateFormat.format(calendar.getTime());
btnDate.setText(selectedDate);
btnDate.setTextColor(getResources().getColor(colorPrimaryDark));
}
};
Date newDate = calendar.getTime();
DatePickerDialog dialog= new DatePickerDialog(this, dateSetListener,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
dialog.getDatePicker().setMinDate(Calendar.getInstance().getTimeInMillis());
dialog.show();
}
首先声明如下:
private Calendar myCalendar = Calendar.getInstance();
然后喜欢你的代码:-
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String selectedDate = dateFormat.format(calendar.getTime());
btnDate.setText(selectedDate);
btnDate.setTextColor(getResources().getColor(colorPrimaryDark));
}
};
在您的代码而不是最后两行中进行此更改:
`Date newDate = calendar.getTime();
new DatePickerDialog(TutorAddNewTimeSlotActivity.this, dateSetListener,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();`
改变它,使它像下面这样:
datePickerDialog.getDatePicker().setMinDate(myCalendar.getTimeInMillis());
datePickerDialog.show();
这将禁用过去的日期。