gpt4 book ai didi

java - 如何将 "todate picker"选定日期设置为 "from datepicker"日期的最大日期

转载 作者:行者123 更新时间:2023-12-02 11:57:43 25 4
gpt4 key购买 nike

我有两个日期选择器From和To,在这里我可以将From datepicker值设置为to datepicker的最小日期。

现在我的要求是,如果我选择To datepicker日期,我想将其设置为From datepicker date的最大日期。我尝试了一种解决方案,但它不适用于我的情况。

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;

Fromdatepicker OnDateSetListener方法

 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);
textviewFrom.setText(fromDate);

toDatePicker.getDatePicker().setMinDate(fromDateCalendar.getTimeInMillis());
}

};

到日期选择器的OnDateSetListener方法

 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);
textviewTo.setText(toDate);
todatepicker.getDatePicker().setMinDate(-2208902400000L);
fromdatePicker.getDatePicker().setMaxDate(toDateCalendar.getTimeInMillis());

}

};


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));

用于调用日期选择器对话框的 TextView 监听器

textviewFrom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {


fromDatePicker.show();
}
});
textviewTo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {


toDatePicker.show();
}
});

我试过了

fromDatePicker.getDatePicker().setMaxDate(toDateCalendar.get‌​TimeInMillis()); 

在toDatePickerListener的onDateSet方法中,但它不是 工作

最佳答案

我尝试了以下方法,希望对你有帮助。

 private String selectedToDate = "";
private String selectedFromDate = "";
private Calendar calToDate, calFromDate;

将此代码写入 onCreate() 方法

calFromDate = convertToCalender(selectedFromDate);
calToDate = convertToCalender(selectedToDate);

Todate 和 FromDate TextView 点击次数

textviewFrom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

showFromDatePicker(fromdateListener, calFromDate, calToDate);
}
});
textviewTo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

showToDatePicker(todateListener, calToDate, calFromDate);
}
});

toDate 和 fromDate 监听器

 public DatePickerDialog.OnDateSetListener fromdateListener =
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calFromDate.set(year, monthOfYear, dayOfMonth);
selectedFromDate = returnFormattedDate(calFromDate.getTimeInMillis());
textviewFrom.setText(selectedFromDate);
}
};

DatePickerDialog.OnDateSetListener todateListener =
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calToDate.set(year, monthOfYear, dayOfMonth);
selectedToDate = returnFormattedDate(calToDate.getTimeInMillis());
textviewTo.setText(selectedToDate);
}
};
private String returnFormattedDate(long timeInMilliSec) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd",
Locale.getDefault());
return dateFormat.format(new Date(timeInMilliSec));
}

显示日期选择器的方法

private void showFromDatePicker(DatePickerDialog.OnDateSetListener listener,
Calendar calendar,
Calendar minDateCalender) {
DatePickerDialog dialog = new DatePickerDialog(getActivity(),
listener,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
if (minDateCalender != null) {
dialog.getDatePicker().setMinDate(minDateCalender.getTimeInMillis());
}
dialog.show();
}

private void showToDatePicker(DatePickerDialog.OnDateSetListener listener,
Calendar calendar,
Calendar maxDateCalender) {
DatePickerDialog dialog = new DatePickerDialog(getActivity(),
listener,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
dialog.getDatePicker().setMaxDate(maxDateCalender.getTimeInMillis());

dialog.show();
}

将字符串日期转换为日历的方法

private Calendar convertToCalender(String date) {
Calendar mCalender = Calendar.getInstance();
if (date == null) {
return mCalender;
} else {
try {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date returnDate = formatter.parse(date);
mCalender = Calendar.getInstance();
mCalender.setTime(returnDate);
return mCalender;
} catch (ParseException e) {
e.printStackTrace();
return mCalender;
}
}
}

关于java - 如何将 "todate picker"选定日期设置为 "from datepicker"日期的最大日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47471785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com