android 工作室从日期选择器中禁用天数
android studio disable days from date picker
我想从日期选择器中禁用某一天
我有一个数组,用于存储我要禁用的日期。
我有这个代码来禁用日历中的日子
for (Calendar loopdate = min_date_c;
min_date_c.before(max_date_c);
min_date_c.add(Calendar.DATE, 1), loopdate = min_date_c) {
int dayOfWeek = loopdate.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.FRIDAY) {
Calendar[] disabledDays = new Calendar[1];
disabledDays[0] = loopdate;
datePickerDialog.setDisabledDays(disabledDays);
}
}
并且我尝试使用此代码来禁用存储在数组列表中的日期,但它不起作用。它只禁用 arraylist
中的第一项
ArrayList a=new ArrayList();
a.add(1); //saturday
a.add(2); //sunday
a.add(3); //monday
int day;
for(int i=0; i<a.size();i++) {
day=(Integer)a.get(i);
for (Calendar loopdate = min_date_c;
min_date_c.before(max_date_c);
min_date_c.add(Calendar.DATE, 1), loopdate = min_date_c) {
int dayOfWeek = loopdate.get(Calendar.DAY_OF_WEEK);
if (day == 1) {
System.out.println(day);
if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.FRIDAY) {
Calendar[] disabledDays = new Calendar[1];
disabledDays[0] = loopdate;
datePickerDialog.setDisabledDays(disabledDays);
}
}
else if (day==2) {
System.out.println(day);
if (dayOfWeek == Calendar.SUNDAY || dayOfWeek == Calendar.FRIDAY) {
Calendar[] disabledDays = new Calendar[1];
disabledDays[0] = loopdate;
datePickerDialog.setDisabledDays(disabledDays);
}
}
else if (day==3) {
System.out.println(day);
if (dayOfWeek == Calendar.MONDAY || dayOfWeek == Calendar.FRIDAY) {
Calendar[] disabledDays = new Calendar[1];
disabledDays[0] = loopdate;
datePickerDialog.setDisabledDays(disabledDays);
}
}
}
我该如何解决这个问题?
我使用这段代码来显示没有天数的日期选择器
DatePickerDialog datePickerDialog = new DatePickerDialog(
RacingRsActivity.this,
android.R.style.Theme_Holo_Dialog,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
dateRc.setText(DateUtil.getCalendarHeaderDate(getDateFromDatePicker(view)));
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
}
},
mYear, mMonth, mDay);
datePickerDialog.getDatePicker().findViewById(getResources().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
datePickerDialog.show();
您可以使用material设计组件MaterialDatePicker
to control the calendar more easily and efficiently; it allows you to control every single day of the calendar by implementing CalendarConstraints.DateValidator
界面。
具体在您的问题中,您需要禁用特定日期;因此,您将创建一个实现 CalendarConstraints.DateValidator
的 class,然后:
- 覆盖所需的方法,并构建一个 Parcelable
CalendarConstraints.DateValidator
Extends Parcelable
in order to maintain the DateValidator
across
device configuration changes. Parcelable
breaks when passed between
processes.
- 覆盖验证每个日期的
isValid(long date)
方法
在日历中和 returns 一个 boolean
表明是否
日期是否有效,换句话说,如果日期启用或disabled/dimmed;因此,如果日期有效,则它 returns 为真,并且该日期将可供用户选择。
在下文中,我将提供一个在 Feb.2020 禁用 Mondays 的验证器;您可以用同样的方式轻松禁用任何一天。
实施CalendarConstraints.DateValidator
:
static class MondaysOutValidator implements CalendarConstraints.DateValidator {
int mYear, mMonth, mDayOfWeek;
MondaysOutValidator(int year, int month, int dayOfWeek) {
mYear = year;
mMonth = month;
mDayOfWeek = dayOfWeek;
}
MondaysOutValidator(Parcel parcel) {
mYear = parcel.readInt();
mMonth = parcel.readInt();
mDayOfWeek = parcel.readInt();
}
@Override
public boolean isValid(long date) {
List<Integer> allXDayOfMonth = getAllXDayOfMonth(mYear, mMonth, mDayOfWeek);
boolean isValidDays = false;
for (int xDay : allXDayOfMonth) {
Calendar calendarStart = Calendar.getInstance();
Calendar calendarEnd = Calendar.getInstance();
ArrayList<Long> minDate = new ArrayList<>();
ArrayList<Long> maxDate = new ArrayList<>();
calendarStart.set(mYear, mMonth, xDay - 1);
calendarEnd.set(mYear, mMonth, xDay);
minDate.add(calendarStart.getTimeInMillis());
maxDate.add(calendarEnd.getTimeInMillis());
isValidDays = isValidDays || !(minDate.get(0) > date || maxDate.get(0) < date);
}
return !isValidDays;
}
private static int getFirstXDayOfMonth(int year, int month, int dayOfWeek) {
Calendar cacheCalendar = Calendar.getInstance();
cacheCalendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
cacheCalendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
cacheCalendar.set(Calendar.MONTH, month);
cacheCalendar.set(Calendar.YEAR, year);
return cacheCalendar.get(Calendar.DATE);
}
private static List<Integer> getAllXDayOfMonth(int year, int month, int dayOfWeek) {
final int ONE_WEEK = 7;
int firstDay = getFirstXDayOfMonth(year, month, dayOfWeek);
List<Integer> xDays = new ArrayList<>();
xDays.add(firstDay);
Calendar calendar = new GregorianCalendar(year, month, firstDay);
calendar.add(Calendar.DAY_OF_MONTH, ONE_WEEK); // adding 1 Week
while (calendar.get(Calendar.MONTH) == month) {
xDays.add(calendar.get(Calendar.DAY_OF_MONTH));
calendar.add(Calendar.DAY_OF_MONTH, ONE_WEEK); // adding 1 Week
}
return xDays;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mYear);
dest.writeInt(mMonth);
dest.writeInt(mDayOfWeek);
}
public static final Parcelable.Creator<MondaysOutValidator> CREATOR = new Parcelable.Creator<MondaysOutValidator>() {
@Override
public MondaysOutValidator createFromParcel(Parcel parcel) {
return new MondaysOutValidator(parcel);
}
@Override
public MondaysOutValidator[] newArray(int size) {
return new MondaysOutValidator[size];
}
};
}
然后使用此验证器构建 CalendarConstraints
以便将这些约束应用于日历日期
/*
* Limit selectable range to days other than Mondays of the month
*/
private CalendarConstraints.Builder mondayDisableConstraints() {
CalendarConstraints.Builder constraintsBuilderRange = new CalendarConstraints.Builder();
constraintsBuilderRange.setValidator(new MondaysOutValidator(2020, Calendar.FEBRUARY, Calendar.MONDAY));
return constraintsBuilderRange;
}
最后在 activity/fragment
中构建 MaterialDatePicker
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MaterialDatePicker.Builder<Long> builderRange = MaterialDatePicker.Builder.datePicker();
builderRange.setCalendarConstraints(mondayDisableConstraints().build());
builderRange.setTitleText("Select Date Range");
MaterialDatePicker<Long> pickerRange = builderRange.build();
pickerRange.show(getSupportFragmentManager(), pickerRange.toString());
}
这是输出:
希望对您有所帮助,欢迎多多支持。
我想从日期选择器中禁用某一天 我有一个数组,用于存储我要禁用的日期。
我有这个代码来禁用日历中的日子
for (Calendar loopdate = min_date_c;
min_date_c.before(max_date_c);
min_date_c.add(Calendar.DATE, 1), loopdate = min_date_c) {
int dayOfWeek = loopdate.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.FRIDAY) {
Calendar[] disabledDays = new Calendar[1];
disabledDays[0] = loopdate;
datePickerDialog.setDisabledDays(disabledDays);
}
}
并且我尝试使用此代码来禁用存储在数组列表中的日期,但它不起作用。它只禁用 arraylist
中的第一项 ArrayList a=new ArrayList();
a.add(1); //saturday
a.add(2); //sunday
a.add(3); //monday
int day;
for(int i=0; i<a.size();i++) {
day=(Integer)a.get(i);
for (Calendar loopdate = min_date_c;
min_date_c.before(max_date_c);
min_date_c.add(Calendar.DATE, 1), loopdate = min_date_c) {
int dayOfWeek = loopdate.get(Calendar.DAY_OF_WEEK);
if (day == 1) {
System.out.println(day);
if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.FRIDAY) {
Calendar[] disabledDays = new Calendar[1];
disabledDays[0] = loopdate;
datePickerDialog.setDisabledDays(disabledDays);
}
}
else if (day==2) {
System.out.println(day);
if (dayOfWeek == Calendar.SUNDAY || dayOfWeek == Calendar.FRIDAY) {
Calendar[] disabledDays = new Calendar[1];
disabledDays[0] = loopdate;
datePickerDialog.setDisabledDays(disabledDays);
}
}
else if (day==3) {
System.out.println(day);
if (dayOfWeek == Calendar.MONDAY || dayOfWeek == Calendar.FRIDAY) {
Calendar[] disabledDays = new Calendar[1];
disabledDays[0] = loopdate;
datePickerDialog.setDisabledDays(disabledDays);
}
}
}
我该如何解决这个问题?
我使用这段代码来显示没有天数的日期选择器
DatePickerDialog datePickerDialog = new DatePickerDialog(
RacingRsActivity.this,
android.R.style.Theme_Holo_Dialog,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
dateRc.setText(DateUtil.getCalendarHeaderDate(getDateFromDatePicker(view)));
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
}
},
mYear, mMonth, mDay);
datePickerDialog.getDatePicker().findViewById(getResources().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
datePickerDialog.show();
您可以使用material设计组件MaterialDatePicker
to control the calendar more easily and efficiently; it allows you to control every single day of the calendar by implementing CalendarConstraints.DateValidator
界面。
具体在您的问题中,您需要禁用特定日期;因此,您将创建一个实现 CalendarConstraints.DateValidator
的 class,然后:
- 覆盖所需的方法,并构建一个 Parcelable
CalendarConstraints.DateValidator
ExtendsParcelable
in order to maintain theDateValidator
across device configuration changes.Parcelable
breaks when passed between processes.
- 覆盖验证每个日期的
isValid(long date)
方法 在日历中和 returns 一个boolean
表明是否 日期是否有效,换句话说,如果日期启用或disabled/dimmed;因此,如果日期有效,则它 returns 为真,并且该日期将可供用户选择。
在下文中,我将提供一个在 Feb.2020 禁用 Mondays 的验证器;您可以用同样的方式轻松禁用任何一天。
实施CalendarConstraints.DateValidator
:
static class MondaysOutValidator implements CalendarConstraints.DateValidator {
int mYear, mMonth, mDayOfWeek;
MondaysOutValidator(int year, int month, int dayOfWeek) {
mYear = year;
mMonth = month;
mDayOfWeek = dayOfWeek;
}
MondaysOutValidator(Parcel parcel) {
mYear = parcel.readInt();
mMonth = parcel.readInt();
mDayOfWeek = parcel.readInt();
}
@Override
public boolean isValid(long date) {
List<Integer> allXDayOfMonth = getAllXDayOfMonth(mYear, mMonth, mDayOfWeek);
boolean isValidDays = false;
for (int xDay : allXDayOfMonth) {
Calendar calendarStart = Calendar.getInstance();
Calendar calendarEnd = Calendar.getInstance();
ArrayList<Long> minDate = new ArrayList<>();
ArrayList<Long> maxDate = new ArrayList<>();
calendarStart.set(mYear, mMonth, xDay - 1);
calendarEnd.set(mYear, mMonth, xDay);
minDate.add(calendarStart.getTimeInMillis());
maxDate.add(calendarEnd.getTimeInMillis());
isValidDays = isValidDays || !(minDate.get(0) > date || maxDate.get(0) < date);
}
return !isValidDays;
}
private static int getFirstXDayOfMonth(int year, int month, int dayOfWeek) {
Calendar cacheCalendar = Calendar.getInstance();
cacheCalendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
cacheCalendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
cacheCalendar.set(Calendar.MONTH, month);
cacheCalendar.set(Calendar.YEAR, year);
return cacheCalendar.get(Calendar.DATE);
}
private static List<Integer> getAllXDayOfMonth(int year, int month, int dayOfWeek) {
final int ONE_WEEK = 7;
int firstDay = getFirstXDayOfMonth(year, month, dayOfWeek);
List<Integer> xDays = new ArrayList<>();
xDays.add(firstDay);
Calendar calendar = new GregorianCalendar(year, month, firstDay);
calendar.add(Calendar.DAY_OF_MONTH, ONE_WEEK); // adding 1 Week
while (calendar.get(Calendar.MONTH) == month) {
xDays.add(calendar.get(Calendar.DAY_OF_MONTH));
calendar.add(Calendar.DAY_OF_MONTH, ONE_WEEK); // adding 1 Week
}
return xDays;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mYear);
dest.writeInt(mMonth);
dest.writeInt(mDayOfWeek);
}
public static final Parcelable.Creator<MondaysOutValidator> CREATOR = new Parcelable.Creator<MondaysOutValidator>() {
@Override
public MondaysOutValidator createFromParcel(Parcel parcel) {
return new MondaysOutValidator(parcel);
}
@Override
public MondaysOutValidator[] newArray(int size) {
return new MondaysOutValidator[size];
}
};
}
然后使用此验证器构建 CalendarConstraints
以便将这些约束应用于日历日期
/*
* Limit selectable range to days other than Mondays of the month
*/
private CalendarConstraints.Builder mondayDisableConstraints() {
CalendarConstraints.Builder constraintsBuilderRange = new CalendarConstraints.Builder();
constraintsBuilderRange.setValidator(new MondaysOutValidator(2020, Calendar.FEBRUARY, Calendar.MONDAY));
return constraintsBuilderRange;
}
最后在 activity/fragment
中构建MaterialDatePicker
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MaterialDatePicker.Builder<Long> builderRange = MaterialDatePicker.Builder.datePicker();
builderRange.setCalendarConstraints(mondayDisableConstraints().build());
builderRange.setTitleText("Select Date Range");
MaterialDatePicker<Long> pickerRange = builderRange.build();
pickerRange.show(getSupportFragmentManager(), pickerRange.toString());
}
这是输出:
希望对您有所帮助,欢迎多多支持。