我有两个DatePicker。一个与日期和其他与迄今。我想要 显示日期总是大于从日期开始根据日期选择。我正在使用Android默认日期选择器,并想要禁用过去 TO datepicker的日期,如果它大于From日期,反之亦然。我知道 这个主题在这里被多次询问过。但是,我无法使我的代码工作 喜欢建议的解决方案,请帮忙吗?
Calendar calendarOne = Calendar.getInstance();
datePickerFrom= new DatePickerDialog(getActivity(), new
DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
StringBuilder selectedDate = new
StringBuilder().append(dayOfMonth).append("-").append(monthOfYear +
1).append("-").append(year);
fromText.setText(selectedDate);
fromText.setTextColor(android.graphics.Color.BLACK);
}
}, calendarOne.get(Calendar.YEAR), calendarOne.get(Calendar.MONTH),
calendarOne.get(Calendar.DAY_OF_MONTH));
fromText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
datePickerFrom.getDatePicker().setMaxDate(new
Date().getTime());
datePickerFrom.show();
}
});
Calendar calendarTwo = Calendar.getInstance();
datePickerTo = new DatePickerDialog(getActivity(), new
DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int
dayOfMonth) {
StringBuilder selectedDate = new
StringBuilder().append(dayOfMonth).append("-").append(monthOfYear +
1).append("-").append(year);
toText.setText(selectedDate);
toText.setTextColor(android.graphics.Color.BLACK);
}
}, calendarTwo.get(Calendar.YEAR), calendarTwo.get(Calendar.MONTH),
calendarTwo.get(Calendar.DAY_OF_MONTH));
toText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
datePickerTo.getDatePicker().setMaxDate(new Date().getTime());
datePickerTo.show();
}
});
}
................................ 更新的代码
final Calendar calendarOne = Calendar.getInstance();
datePickerFrom= new DatePickerDialog(getActivity(), new
DatePickerDialog.OnDateSetListener()
{
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onDateSet(DatePicker view, int year, final int
monthOfYear, int dayOfMonth)
{
StringBuilder selectedDate = new
StringBuilder().append(dayOfMonth).append("-
").append(monthOfYear + 1).append("-").append(year);
fromText.setText(selectedDate);
fromText.setTextColor(android.graphics.Color.BLACK);
Calendar calendarTwo = Calendar.getInstance();
datePickerTo = new DatePickerDialog(getActivity());
datePickerTo.getDatePicker().setMinDate(calendarOne.getTimeInMillis());
datePickerTo = new DatePickerDialog(activity, new
DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int
month, int dayOfMonth) {
StringBuilder selectedDate = new
StringBuilder().append(dayOfMonth).append("-").append(monthOfYear +
1).append("-").append(year);
toText.setText(selectedDate);
toText.setTextColor(android.graphics.Color.BLACK);
}
}, calendarTwo.get(Calendar.YEAR),
calendarTwo.get(Calendar.MONTH),
calendarTwo.get(Calendar.DAY_OF_MONTH));
toText.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
datePickerTo.getDatePicker().setMaxDate(new
Date().getTime());
datePickerTo.show();
}
});
}
}, calendarOne.get(Calendar.YEAR),
calendarOne.get(Calendar.MONTH),
calendarOne.get(Calendar.DAY_OF_MONTH));
fromText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
datePickerFrom.getDatePicker().setMaxDate(new
Date().getTime());
datePickerFrom.show();
}
});
答案 0 :(得分:2)
首先在onCreate之前在课堂上声明以下内容:
private String fromDate;
private DatePickerDialog fromDatePicker;
private DatePickerDialog.OnDateSetListener fromDatePickerListener;
private Calendar fromDateCalendar;
private String toDate;
private DatePickerDialog toDatePicker;
private DatePickerDialog.OnDateSetListener toDatePickerListener;
private Calendar toDateCalendar;
之后在onCreate方法中添加以下代码:
fromDateCalendar = Calendar.getInstance();
fromDatePickerListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
fromDateCalendar.set(Calendar.YEAR, year);
fromDateCalendar.set(Calendar.MONTH, monthOfYear);
fromDateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
fromDate = fromDateCalendar.get(Calendar.YEAR) + "-" + (fromDateCalendar.get(Calendar.MONTH)+1) + "-" + fromDateCalendar.get(Calendar.DAY_OF_MONTH);
//etFromDate.setText(fromDate);
toDatePicker.getDatePicker().setMinDate(fromDateCalendar.getTimeInMillis());
}
};
toDateCalendar = Calendar.getInstance();
toDatePickerListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
toDateCalendar.set(Calendar.YEAR, year);
toDateCalendar.set(Calendar.MONTH, monthOfYear);
toDateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
toDate = toDateCalendar.get(Calendar.YEAR) + "-" + (toDateCalendar.get(Calendar.MONTH)+1) + "-" + toDateCalendar.get(Calendar.DAY_OF_MONTH);
//etToDate.setText(toDate);
}
};
toDatePicker = new DatePickerDialog(getContext(),toDatePickerListener, toDateCalendar.get(Calendar.YEAR), toDateCalendar.get(Calendar.MONTH), toDateCalendar.get(Calendar.DAY_OF_MONTH));
fromDatePicker = new DatePickerDialog(getContext(),fromDatePickerListener, fromDateCalendar.get(Calendar.YEAR), fromDateCalendar.get(Calendar.MONTH), fromDateCalendar.get(Calendar.DAY_OF_MONTH));
下一步是当您想要在点击按钮时显示datePicker或编辑文本时,您想要显示日期选择器,请使用以下行显示:
fromDatePicker.show();
toDatePicker.show();
如果你想在textView或editText中的任何地方展示它们,你也可以使用fromDate和toDate字符串来显示所选日期。