自定义android日历

华子-android 2016-03-25 02:54:39
最近项目中经常需要用到日历,而网络上找到的三方日历往往不符合项目需求,于是,我选择了自己写一个日历控件。我项目中我总共定义了两个日历,一个比较简单,一个比较复杂。要创建一个日历,我觉得得从两个个方面考虑。首先,日历分为大小月,还有一个特殊的月份2月,大小月,自然不用说,2月就得看年份是不是闰月,是的话就是29天,不是得话就是28天;其次,就是一个月的第一天使周几,我这里是通过获取系统的日历设置月份,并且把日期设置为月份的第一天来获取到第一天是周几。好了不说废话了,我先来说说具体代码吧。
首先获取一个本地日历,这个日历肯定是当天的日期数据。
Calendar instance = Calendar.getInstance();
//获取当前年份
currentYear = instance.get(Calendar.YEAR);
currentShowYear=currentYear;
//获取当前月份
currentMonth = instance.get(Calendar.MONTH);
//这里为什么要加1,我想打架应该能猜到,因为外国人是从0开始,而我们习惯是从1开始,所以返回的月份肯定是比我们少一
currentMonth=currentMonth+1;
realCurrentMonth=currentMonth;
//获取当前的日期中的日
dayOfMonth = instance.get(Calendar.DAY_OF_MONTH);
然后通过月份计算天数
private void setDays(int currentYear,int currentMonth) {
list.clear();
if(currentMonth ==1|| currentMonth ==3|| currentMonth ==5|| currentMonth ==7|| currentMonth ==8|| currentMonth ==10|| currentMonth ==12){
setBigMonth(list);
}else if(currentMonth ==2){
set2Month(list, currentYear);
}else{
setSmallMonth(list);
}
}


private void setSmallMonth(List<String> list) {
for(int i=0;i<30;i++){
list.add((i+1)+"");
}
}

private void setBigMonth(List<String> list) {
for(int i=0;i<31;i++){
list.add((i+1)+"");
}
}

private void set2Month(List<String> list,int currentYear) {
if(currentYear%4==0){
for(int i=0;i<29;i++){
list.add((i+1)+"");
}
}else{
for(int i=0;i<28;i++){
list.add((i+1)+"");
}

}
}
然后计算每月的第一天是周几
instance.set(Calendar.DAY_OF_MONTH, 1);
dayOfWeekInMonth = instance.get(Calendar.DAY_OF_WEEK);
这样需要的数据基本就齐全了。接下来就是一系列的计算了,直接上代码
点击上下月时
public int setDate(int addOrDown,int year){
Calendar instance = Calendar.getInstance();
currentMonth = currentMonth+addOrDown;
if(currentMonth>12){
instance.set(Calendar.YEAR,year+1);
instance.set(Calendar.MONTH,0);
currentMonth=1;
}else if(currentMonth<1){
instance.set(Calendar.YEAR,year-1);
instance.set(Calendar.MONTH,11);
currentMonth=12;
}else{
instance.set(Calendar.YEAR,year);
instance.set(Calendar.MONTH,currentMonth-1);
}

//当前显示年份在真是年份之后
if(instance.get(Calendar.YEAR)>currentYear){
isBeforeCurrentMonth=false;
isCurrentMonth=false;
//当前显示年份就是真实年份
}else if(instance.get(Calendar.YEAR)==currentYear){
if(currentMonth>realCurrentMonth){
isBeforeCurrentMonth=false;
isCurrentMonth=false;
}else if(currentMonth==realCurrentMonth){
isBeforeCurrentMonth=true;
isCurrentMonth=true;
}else{
isBeforeCurrentMonth=true;
isCurrentMonth=false;
}
//当前显示年份在真实年份之前
}else{
isBeforeCurrentMonth=true;
isCurrentMonth=false;
}

calendarAdapter.setIsCurrentMonth(isCurrentMonth);
calendarAdapter.setIsBeforeCurrentMonth(isBeforeCurrentMonth);

//获取当月第一天周几
instance.set(Calendar.DAY_OF_MONTH, 1);
dayOfWeekInMonth=instance.get(Calendar.DAY_OF_WEEK);
calendarAdapter.setDayOfWeek(dayOfWeekInMonth);

String[] split = selectedDate.split("-");
if(Integer.valueOf(split[0])==instance.get(Calendar.YEAR) && Integer.valueOf(split[1])==currentMonth){
calendarAdapter.setSelectedDay(Integer.valueOf(split[2]));
}else{
calendarAdapter.setSelectedDay(0);
}

currentShowYear=instance.get(Calendar.YEAR);
return currentShowYear;
}


...全文
218 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
华子-android 2016-05-23
  • 打赏
  • 举报
回复
实例下载地址 http://download.csdn.net/detail/qq_26491053/9525462

80,352

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧