Android - 为开始日期和结束日期多次更新 DatePickers 的 minDate 和 maxDate
Posted
技术标签:
【中文标题】Android - 为开始日期和结束日期多次更新 DatePickers 的 minDate 和 maxDate【英文标题】:Android - Update minDate and maxDate of a DatePickers many times for Start date and End date 【发布时间】:2015-04-02 14:09:55 【问题描述】:我有两个用于 ToDate[start] 和 FromDate[end] 的 Datepickers 对话框。
我使用 setMaxDate() 函数将 datePicker 的最大日期设置为当前日期。
我的问题是当
1) 我要更改 ToDatePicker 的日期,我不会设置 FromDatePicker 的 setMaxDate()。
2) 我要更改 FromDatePicker 的日期,我不会设置 ToDatePicker 的 setMinDate()。
在我的代码中,这是第一次工作,但下次当我更改两个或单个日期选择器时,它没有设置 setMaxDate()、setMinDate() 函数
这是我的代码
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
Calendar newCalendar = Calendar.getInstance();
//Setting first None value to updatedDate To and from
Util.SaveToSharedPref(MgntNCActivity.this,
Constant.ShaPreMgntNcFilter,
Constant.ShaPreMgntNcFilterUpdatedToDate, Constant.NoValue);
Util.SaveToSharedPref(MgntNCActivity.this,
Constant.ShaPreMgntNcFilter,
Constant.ShaPreMgntNcFilterUpdatedFromDate, Constant.NoValue);
datePickerDiaFrom=new DatePickerDialog(MgntNCActivity.this, new DatePickerDialog.OnDateSetListener()
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth)
Calendar upDateFrom = Calendar.getInstance();
upDateFrom.set(year, monthOfYear, dayOfMonth);
txtFromUpdatedDate.setText(dateFormatter.format(upDateFrom.getTime()));
// System.out.println("Date 1 )GTM : "+upDateTo.getTime().toGMTString());
//setting minimum limit to date Picker 'To'
if (dateFormatter.format(upDateFrom.getTime()).equals(dateFormatter.format(NetworkUtil.getTodayDate())))
upDateFrom.set(Calendar.HOUR_OF_DAY, upDateFrom.getMinimum(Calendar.HOUR_OF_DAY));
upDateFrom.set(Calendar.MINUTE, upDateFrom.getMinimum(Calendar.MINUTE));
upDateFrom.set(Calendar.SECOND, upDateFrom.getMinimum(Calendar.SECOND));
upDateFrom.set(Calendar.MILLISECOND, upDateFrom.getMinimum(Calendar.MILLISECOND));
datePickerDiaTo.getDatePicker().setMinDate(upDateFrom.getTimeInMillis());
System.out.println("setting To DatePicker current date as minDate : "
+dateFormatter.format(NetworkUtil.getTodayDate()));
else
datePickerDiaTo.getDatePicker().setMinDate(upDateFrom.getTimeInMillis());
System.out.println("setting To DatePicker minDate : "
+dateFormatter.format(upDateFrom.getTime()));
//datePickerDiaTo.getDatePicker().setMinDate(upDateFrom.getTimeInMillis());
Util.SaveToSharedPref(MgntNCActivity.this,
Constant.ShaPreMgntNcFilter,
Constant.ShaPreMgntNcFilterUpdatedFromDate, dateFormatter.format(upDateFrom.getTime()));
, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDiaTo=new DatePickerDialog(MgntNCActivity.this, new DatePickerDialog.OnDateSetListener()
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth)
Calendar upDateTo = Calendar.getInstance();
upDateTo.set(year, monthOfYear, dayOfMonth);
txtToUpdatedDate.setText(dateFormatter.format(upDateTo.getTime()));
// System.out.println("Date 1 )GTM : "+upDateTo.getTime().toGMTString());
//setting maximum limit to date Picker 'From'
if (dateFormatter.format(upDateTo.getTime()).equals(dateFormatter.format(NetworkUtil.getTodayDate())))
upDateTo.set(Calendar.HOUR_OF_DAY, upDateTo.getMaximum(Calendar.HOUR_OF_DAY));
upDateTo.set(Calendar.MINUTE, upDateTo.getMaximum(Calendar.MINUTE));
upDateTo.set(Calendar.SECOND, upDateTo.getMaximum(Calendar.SECOND));
upDateTo.set(Calendar.MILLISECOND, upDateTo.getMaximum(Calendar.MILLISECOND));
datePickerDiaFrom.getDatePicker().setMaxDate(upDateTo.getTimeInMillis());
System.out.println("setting From DatePicker current date as maxDate : "
+dateFormatter.format(NetworkUtil.getTodayDate()));
else
datePickerDiaFrom.getDatePicker().setMaxDate(upDateTo.getTimeInMillis());
System.out.println("setting From DatePicker maxDate : "
+dateFormatter.format(upDateTo.getTime()));
//datePickerDiaFrom.getDatePicker().setMaxDate(upDateTo.getTimeInMillis());
Util.SaveToSharedPref(MgntNCActivity.this,
Constant.ShaPreMgntNcFilter,
Constant.ShaPreMgntNcFilterUpdatedToDate, dateFormatter.format(upDateTo.getTime()));
, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
//setting max limit to date Picker
datePickerDiaTo.getDatePicker().setMaxDate(NetworkUtil.getTodayDate().getTime());
datePickerDiaFrom.getDatePicker().setMaxDate(NetworkUtil.getTodayDate().getTime());
任何人请建议我解决这个问题... 谢谢...
【问题讨论】:
【参考方案1】:我终于找到了解决办法:)
我正在编写 datePickerDialogs 'datePickerDiaFrom' , 'datePickerDiaTo' 是类变量,这就是为什么当我当时更改 minDate 或 maxDate 时它不会替换以前的日期。
我的解决方案是,
每次我点击 textview 以打开新的 dialogPicker 并为其设置范围。
这是我的代码:
static long MaximumDate=0,MinimunDate=0;
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
View.OnClickListener showDatePicker = new View.OnClickListener()
@Override
public void onClick(View v)
final View vv = v;
Calendar newCalendar=Calendar.getInstance();
switch (vv.getId())
case R.id.txtFromUpdatedDate_MgntNC:
DatePickerDialog dialogFrom=new DatePickerDialog(MgntNCActivity.this, new OnDateSetListener()
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth)
Calendar upDateFrom = Calendar.getInstance();
upDateFrom.set(year, monthOfYear, dayOfMonth);
txtFromUpdatedDate.setText(dateFormatter.format(upDateFrom.getTime()));
MinimunDate=upDateFrom.getTimeInMillis();
, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
if (MaximumDate!=0)
dialogFrom.getDatePicker().setMaxDate(MaximumDate);
else
dialogFrom.getDatePicker().setMaxDate(NetworkUtil.getTodayDate().getTime());
dialogFrom.show();
dialogFrom.setOnCancelListener(new DialogInterface.OnCancelListener()
@Override
public void onCancel(DialogInterface arg0)
MinimunDate=0;
txtFromUpdatedDate.setText(Constant.NoValue);
);
break;
case R.id.txtToUpdatedDate_MgntNC:
DatePickerDialog dialogTo=new DatePickerDialog(MgntNCActivity.this, new OnDateSetListener()
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth)
Calendar upDateTo = Calendar.getInstance();
upDateTo.set(year, monthOfYear, dayOfMonth);
txtToUpdatedDate.setText(dateFormatter.format(upDateTo.getTime()));
MaximumDate=upDateTo.getTimeInMillis();
, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
dialogTo.getDatePicker().setMaxDate(NetworkUtil.getTodayDate().getTime());
if (MinimunDate!=0)
dialogTo.getDatePicker().setMinDate(MinimunDate);
else
//dialog1.getDatePicker().setMinDate(NetworkUtil.getTodayDate().getTime());
dialogTo.show();
dialogTo.setOnCancelListener(new DialogInterface.OnCancelListener()
@Override
public void onCancel(DialogInterface arg0)
MaximumDate=0;
);
break;
default:
break;
;
txtFromUpdatedDate.setOnClickListener(showDatePicker);
txtToUpdatedDate.setOnClickListener(showDatePicker);
【讨论】:
下次有解决方法时要设置 minDate,请在此处提供***.com/questions/19616575/…。只需设置 mPicker.setMinDate(0); @xrnd,不知何故,这种解决方法对我不起作用。 mPicker.setMinDate(0)以上是关于Android - 为开始日期和结束日期多次更新 DatePickers 的 minDate 和 maxDate的主要内容,如果未能解决你的问题,请参考以下文章
如何在 android 日期选择器中将特定选定日期设置为最小日期? (爪哇)