gpt4 book ai didi

Android Material Calendar View 如何用圆角背景高亮显示日期范围?

转载 作者:行者123 更新时间:2023-12-05 00:16:49 24 4
gpt4 key购买 nike

目前我正在使用来自 github 的多产交互式 Material Calendar View。我能够设置我想要的日期的 setBackgroundDrawable。但是现在我想设置日期范围的 setBackgroundDrawable,如下图所示:

enter image description here

当我得到日期范围时,如何实现这种圆角图像背景。示例从 2014 年 3 月 18 日到 2014 年 3 月 22 日。

链接:

https://github.com/prolificinteractive/material-calendarview

最佳答案

我是这样实现的:

enter image description here

在这里找到示例代码-> https://github.com/Thanvandh/Date-Range-Highlight

MainActivity.java

final List<String> pinkDateList = Arrays.asList(
"2019-01-01",
"2019-01-03", "2019-01-04", "2019-01-05", "2019-01-06");
final List<String> grayDateList = Arrays.asList(
"2019-01-09", "2019-01-10", "2019-01-11",
"2019-01-24", "2019-01-25", "2019-01-26", "2019-01-27", "2019-01-28", "2019-01-29");

final String DATE_FORMAT = "yyyy-MM-dd";

int pink = 0;
int gray = 1;

MaterialCalendarView calendarView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

calendarView = findViewById(R.id.calendarView);
calendarView.setShowOtherDates(MaterialCalendarView.SHOW_ALL);

final LocalDate min = getLocalDate("2019-01-01");
final LocalDate max = getLocalDate("2019-12-30");

calendarView.state().edit().setMinimumDate(min).setMaximumDate(max).commit();


setEvent(pinkDateList, pink);
setEvent(grayDateList, gray);

calendarView.invalidateDecorators();
}

void setEvent(List<String> dateList, int color) {
List<LocalDate> localDateList = new ArrayList<>();

for (String string : dateList) {
LocalDate calendar = getLocalDate(string);
if (calendar != null) {
localDateList.add(calendar);
}
}


List<CalendarDay> datesLeft = new ArrayList<>();
List<CalendarDay> datesCenter = new ArrayList<>();
List<CalendarDay> datesRight = new ArrayList<>();
List<CalendarDay> datesIndependent = new ArrayList<>();


for (LocalDate localDate : localDateList) {

boolean right = false;
boolean left = false;

for (LocalDate day1 : localDateList) {


if (localDate.isEqual(day1.plusDays(1))) {
left = true;
}
if (day1.isEqual(localDate.plusDays(1))) {
right = true;
}
}

if (left && right) {
datesCenter.add(CalendarDay.from(localDate));
} else if (left) {
datesLeft.add(CalendarDay.from(localDate));
} else if (right) {
datesRight.add(CalendarDay.from(localDate));
} else {
datesIndependent.add(CalendarDay.from(localDate));
}
}

if (color == pink) {
setDecor(datesCenter, R.drawable.p_center);
setDecor(datesLeft, R.drawable.p_left);
setDecor(datesRight, R.drawable.p_right);
setDecor(datesIndependent, R.drawable.p_independent);
} else {
setDecor(datesCenter, R.drawable.g_center);
setDecor(datesLeft, R.drawable.g_left);
setDecor(datesRight, R.drawable.g_right);
setDecor(datesIndependent, R.drawable.g_independent);
}
}

void setDecor(List<CalendarDay> calendarDayList, int drawable) {
calendarView.addDecorators(new EventDecorator(MainActivity.this
, drawable
, calendarDayList));
}

LocalDate getLocalDate(String date) {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH);
try {
Date input = sdf.parse(date);
Calendar cal = Calendar.getInstance();
cal.setTime(input);
return LocalDate.of(cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH) + 1,
cal.get(Calendar.DAY_OF_MONTH));


} catch (NullPointerException e) {
return null;
} catch (ParseException e) {
return null;
}
}

EventDecorator.java

public class EventDecorator implements DayViewDecorator {
Context context;
private int drawable;
private HashSet<CalendarDay> dates;

public EventDecorator(Context context, int drawable, List<CalendarDay> calendarDays1) {
this.context = context;
this.drawable = drawable;
this.dates = new HashSet<>(calendarDays1);
}

@Override
public boolean shouldDecorate(CalendarDay day) {
return dates.contains(day);
}

@Override
public void decorate(DayViewFacade view) {
// apply drawable to dayView
view.setSelectionDrawable(context.getResources().getDrawable(drawable));
// white text color
view.addSpan(new ForegroundColorSpan(Color.WHITE));
}
}

关于Android Material Calendar View 如何用圆角背景高亮显示日期范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52396913/

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