我有ToDate [start]和FromDate [end]的两个Datepickers对话框。
我使用setMaxDate()函数将datePicker max date设置为当前日期。
我的问题是什么时候
1)我改变ToDatePicker的日期我不会设置FromDatePicker的setMaxDate()。
2)我改变FromDatePicker的日期我不会设置ToDatePicker的setMinDate()。
在我的代码中,这项工作第一次,但下次当我更改两个或单个datepicker时,它没有设置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());
任何人请建议我解决这个问题...... 谢谢......
答案 0 :(得分:3)
最后我找到了解决办法:)
我写的是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);