I want to disable the past dates of date picker in android. i can do it by using
我想禁用android中日期选择器的过去日期。我可以用
dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
This is working fine and the past dates in date picker looks disabled. But I can still click on a previous date and select it. How to not let that happen ? below is the screenshot of my date picker :-
这工作正常,日期选择器中的过去日期看起来是无效的。但是我仍然可以点击之前的日期并选择它。怎么能不让这种事发生呢?下面是我的约会挑选者的截图:-
And here is my code where I am disabling the past dates :-
这是我的代码,我正在禁用过去的日期:-
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
mActivity = new WeakReference<CreateEvent>(
(CreateEvent) getActivity());
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
if(callingView==fromDate){
dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
}else if (callingView==toDate){
dialog.getDatePicker().setMinDate(fromD);
}
// Create a new instance of DatePickerDialog and return it
return dialog;
}
4 个解决方案
#1
8
Got to know from a comment that this is a bug in Lollipop. So, fixed it programatically.
我从一篇评论中得知这是棒棒糖的一个缺陷。所以,固定它编程。
All you need to do is check the selected date with the min date set.
您需要做的就是使用最小日期集检查所选的日期。
#2
4
Yes it is a bug and you need to check the selected date using java code.
是的,它是一个bug,您需要使用java代码检查选定的日期。
For past Date
对过去的日期
public static boolean validatePastDate(Context mContext,int day,int month,int year){
final Calendar c = Calendar.getInstance();
int currentYear = c.get(Calendar.YEAR);
int currentMonth = c.get(Calendar.MONTH)+1;
int currentDay = c.get(Calendar.DAY_OF_MONTH);
if (day > currentDay && year == currentYear && month == currentMonth) {
Toast.makeText(mContext, "Please select valid date", Toast.LENGTH_LONG).show();
return false;
} else if (month > currentMonth && year == currentYear) {
Toast.makeText(mContext, "Please select valid month", Toast.LENGTH_LONG).show();
return false;
} else if (year > currentYear) {
Toast.makeText(mContext, "Please select valid year", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
For Future Date
为未来的日期
public static boolean validateFutureDate(Context mContext,int day,int month,int year){
final Calendar c = Calendar.getInstance();
int currentYear = c.get(Calendar.YEAR);
int currentMonth = c.get(Calendar.MONTH)+1;
int currentDay = c.get(Calendar.DAY_OF_MONTH);
if (day < currentDay && year == currentYear && month == currentMonth) {
Toast.makeText(mContext, "Please select valid date", Toast.LENGTH_LONG).show();
return false;
} else if (month < currentMonth && year == currentYear) {
Toast.makeText(mContext, "Please select valid month", Toast.LENGTH_LONG).show();
return false;
} else if (year < currentYear) {
Toast.makeText(mContext, "Please select valid year", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
Just pass the selected date month ,year in this method and will return boolean value.
在此方法中,只需通过选定的日期月、年并返回布尔值。
#3
0
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
#4
0
To resolve bug for Android Lollipop you can validate date as below:
要解决Android棒棒糖的bug,您可以验证日期如下:
int mYear,mMonth,mDay;
Calendar mcurrentDate = Calendar.getInstance();
mYear = mcurrentDate.get(Calendar.YEAR);
mMonth = mcurrentDate.get(Calendar.MONTH);
mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
// TODO Auto-generated method stub
// validate here with condition to avoid to select past dates
if (selectedyear == mYear && (selectedmonth+1) == mMonth+1) {
if(selectedday < mDay){
Toast.makeText(context, "invalid date", Toast.LENGTH_LONG).show();
return;
}
}
textview.setText(selectedday + "/" + (selectedmonth+1) + "/" + selectedyear);
}
}, mYear, mMonth, mDay);
mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
if(!mDatePicker.isShowing()){
mDatePicker.show();
}
#1
8
Got to know from a comment that this is a bug in Lollipop. So, fixed it programatically.
我从一篇评论中得知这是棒棒糖的一个缺陷。所以,固定它编程。
All you need to do is check the selected date with the min date set.
您需要做的就是使用最小日期集检查所选的日期。
#2
4
Yes it is a bug and you need to check the selected date using java code.
是的,它是一个bug,您需要使用java代码检查选定的日期。
For past Date
对过去的日期
public static boolean validatePastDate(Context mContext,int day,int month,int year){
final Calendar c = Calendar.getInstance();
int currentYear = c.get(Calendar.YEAR);
int currentMonth = c.get(Calendar.MONTH)+1;
int currentDay = c.get(Calendar.DAY_OF_MONTH);
if (day > currentDay && year == currentYear && month == currentMonth) {
Toast.makeText(mContext, "Please select valid date", Toast.LENGTH_LONG).show();
return false;
} else if (month > currentMonth && year == currentYear) {
Toast.makeText(mContext, "Please select valid month", Toast.LENGTH_LONG).show();
return false;
} else if (year > currentYear) {
Toast.makeText(mContext, "Please select valid year", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
For Future Date
为未来的日期
public static boolean validateFutureDate(Context mContext,int day,int month,int year){
final Calendar c = Calendar.getInstance();
int currentYear = c.get(Calendar.YEAR);
int currentMonth = c.get(Calendar.MONTH)+1;
int currentDay = c.get(Calendar.DAY_OF_MONTH);
if (day < currentDay && year == currentYear && month == currentMonth) {
Toast.makeText(mContext, "Please select valid date", Toast.LENGTH_LONG).show();
return false;
} else if (month < currentMonth && year == currentYear) {
Toast.makeText(mContext, "Please select valid month", Toast.LENGTH_LONG).show();
return false;
} else if (year < currentYear) {
Toast.makeText(mContext, "Please select valid year", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
Just pass the selected date month ,year in this method and will return boolean value.
在此方法中,只需通过选定的日期月、年并返回布尔值。
#3
0
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
#4
0
To resolve bug for Android Lollipop you can validate date as below:
要解决Android棒棒糖的bug,您可以验证日期如下:
int mYear,mMonth,mDay;
Calendar mcurrentDate = Calendar.getInstance();
mYear = mcurrentDate.get(Calendar.YEAR);
mMonth = mcurrentDate.get(Calendar.MONTH);
mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
// TODO Auto-generated method stub
// validate here with condition to avoid to select past dates
if (selectedyear == mYear && (selectedmonth+1) == mMonth+1) {
if(selectedday < mDay){
Toast.makeText(context, "invalid date", Toast.LENGTH_LONG).show();
return;
}
}
textview.setText(selectedday + "/" + (selectedmonth+1) + "/" + selectedyear);
}
}, mYear, mMonth, mDay);
mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
if(!mDatePicker.isShowing()){
mDatePicker.show();
}