因为工作功能需求,自定义一个日历,效果如下,点击选中日历
使用github上面一个前辈的框架
implementation 'com.necer.ncalendar:ncalendar:5.0.0' implementation 'com.github.codingending:popuplayout:v1.0'//poplayout
框架使用基本类型地址,大家可以根据需要学习修改:
自定义日历的xml文件
mainactivity,日历的功能重写也是在和这个函数中
package com.example.calendartest; import androidx.appcompat.app.appcompatactivity; import android.os.bundle; import android.os.handler; import android.view.view; import android.widget.button; import android.widget.textview; import com.codingending.popuplayout.popuplayout; import com.necer.calendar.basecalendar; import com.necer.calendar.monthcalendar; import com.necer.enumeration.checkmodel; import com.necer.enumeration.datechangebehavior; import com.necer.listener.oncalendarchangedlistener; import org.joda.time.localdate; public class mainactivity extends appcompatactivity { popuplayout popuplayout; view calendarview; textview myear, mmonth, lastyear, nextyear, lastmonth, nextmonth; monthcalendar monthcalendar; int currentyear, currentmonth; button intent; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); intent = findviewbyid(r.id.intent); calendarview = view.inflate(this, r.layout.calendar, null); popuplayout = popuplayout.init(this, calendarview); } public void intent(view view) { initcalendar(); popuplayout.show(popuplayout.position_center); } public void initcalendar() { monthcalendar = calendarview.findviewbyid(r.id.month_calendar); myear = calendarview.findviewbyid(r.id.year); mmonth = calendarview.findviewbyid(r.id.month); lastyear = calendarview.findviewbyid(r.id.lastyear); nextyear = calendarview.findviewbyid(r.id.nextyear); lastmonth = calendarview.findviewbyid(r.id.lastmonth); nextmonth = calendarview.findviewbyid(r.id.nextmonth); monthcalendar.setcheckmode(checkmodel.single_default_unchecked); monthcalendar.setoncalendarchangedlistener(new oncalendarchangedlistener() { @override public void oncalendarchange(basecalendar basecalendar, int year, int month, localdate localdate, datechangebehavior datechangebehavior) { myear.settext(string.valueof(year)); mmonth.settext(string.valueof(month)); intent.settext(string.valueof(localdate)); currentyear = year; currentmonth = month; new handler().postdelayed(new runnable() { @override public void run() { popuplayout.dismiss(); } },800); } }); } public void lastmonth(view view) { monthcalendar.tolastpager(); } public void nextmonth(view view) { monthcalendar.tonextpager(); } public void nextyear(view view) { monthcalendar.jumpdate(currentyear 1, currentmonth, 1); } public void lastyear(view view) { monthcalendar.jumpdate(currentyear - 1, currentmonth, 1); } }
github