Android使用GridView实现日历功能示例(详细代码)
android使用gridview实现日历功能示例,代码有点多,发个图先:
如果懒得往下看的,可以直接下载源码吧,最近一直有人要,由于时间太久了,懒得找出来整理,今天又看到有人要,正好没事就整理了一下
布局文件:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <linearlayout android:id="@+id/lay_title" style="@style/titlestyle" android:layout_width="fill_parent" > <textview android:id="@+id/btn_back" style="@style/titleleftbuttonstyle" /> <textview android:id="@+id/tv_top" style="@style/titlemiddletextstyle" android:text="请选择日期" /> <textview android:id="@+id/btn_goback_to_today" style="@style/titlerightbuttonstyle" android:text="今天" /> </linearlayout> <linearlayout android:layout_width="fill_parent" android:layout_height="53.0dip" android:background="#ff394147" android:gravity="center_vertical" > <linearlayout android:id="@+id/btn_prev_month" android:layout_width="0.0dip" android:layout_height="fill_parent" android:layout_weight="0.2" android:gravity="center" android:orientation="vertical" > <textview android:id="@+id/left_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/triangle06_states" android:duplicateparentstate="true" android:text="" android:textsize="16.0dip" /> </linearlayout> <linearlayout android:layout_width="0.0dip" android:layout_height="fill_parent" android:layout_weight="0.6" android:gravity="center" android:orientation="vertical" > <textview android:id="@+id/tv_month" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="xxxx年14月" android:textcolor="#ffffffff" android:textsize="21.0dip" /> </linearlayout> <linearlayout android:id="@+id/btn_next_month" android:layout_width="0.0dip" android:layout_height="fill_parent" android:layout_weight="0.2" android:gravity="center" android:orientation="vertical" > <textview android:id="@+id/right_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/triangle05_states" android:duplicateparentstate="true" android:text="" android:textsize="16.0dip" /> </linearlayout> </linearlayout> <linearlayout android:layout_width="fill_parent" android:layout_height="22.0dip" android:background="#ff8091a8" > <textview android:layout_width="0.0dip" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1.0" android:gravity="center" android:text="周日" android:textcolor="#ffffffff" android:textsize="14.0dip" /> <textview android:layout_width="0.0dip" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1.0" android:gravity="center" android:text="周一" android:textcolor="#ffffffff" android:textsize="14.0dip" /> <textview android:layout_width="0.0dip" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1.0" android:gravity="center" android:text="周二" android:textcolor="#ffffffff" android:textsize="14.0dip" /> <textview android:layout_width="0.0dip" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1.0" android:gravity="center" android:text="周三" android:textcolor="#ffffffff" android:textsize="14.0dip" /> <textview android:layout_width="0.0dip" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1.0" android:gravity="center" android:text="周四" android:textcolor="#ffffffff" android:textsize="14.0dip" /> <textview android:layout_width="0.0dip" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1.0" android:gravity="center" android:text="周五" android:textcolor="#ffffffff" android:textsize="14.0dip" /> <textview android:layout_width="0.0dip" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1.0" android:gravity="center" android:text="周六" android:textcolor="#ffffffff" android:textsize="14.0dip" /> </linearlayout> <gridview android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#00000000" android:clickable="true" android:clipchildren="true" android:columnwidth="55.0dip" android:horizontalspacing="1.0dip" android:listselector="@null" android:numcolumns="7" android:paddingbottom="1.0dip" android:stretchmode="columnwidth" android:verticalspacing="1.0dip" /> </linearlayout>
item:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="#ffffff" android:layout_width="fill_parent" android:layout_height="fill_parent"> <textview android:gravity="center" android:layout_width="46dp" android:layout_height="55dp" android:id="@+id/tvtext" /> </linearlayout>
adapter:
/** * 日历gridview中的每一个item显示的textview * @author lmw * */ public class calendaradapter extends baseadapter { private boolean isleapyear = false; //是否为闰年 private int daysofmonth = 0; //某月的天数 private int dayofweek = 0; //具体某一天是星期几 private int lastdaysofmonth = 0; //上一个月的总天数 private context context; private string[] daynumber = new string[42]; //一个gridview中的日期存入此数组中 // private static string week[] = {"周日","周一","周二","周三","周四","周五","周六"}; private specialcalendar sc = null; private lunarcalendar lc = null; private resources res = null; private drawable drawable = null; private string currentyear = ""; private string currentmonth = ""; private string currentday = ""; private simpledateformat sdf = new simpledateformat("yyyy-m-d"); private int currentflag = -1; //用于标记当天 private int[] schdatetagflag = null; //存储当月所有的日程日期 private string showyear = ""; //用于在头部显示的年份 private string showmonth = ""; //用于在头部显示的月份 private string animalsyear = ""; private string leapmonth = ""; //闰哪一个月 private string cyclical = ""; //天干地支 //系统当前时间 private string sysdate = ""; private string sys_year = ""; private string sys_month = ""; private string sys_day = ""; public calendaradapter(){ date date = new date(); sysdate = sdf.format(date); //当期日期 sys_year = sysdate.split("-")[0]; sys_month = sysdate.split("-")[1]; sys_day = sysdate.split("-")[2]; } public calendaradapter(context context,resources rs,int jumpmonth,int jumpyear,int year_c,int month_c,int day_c){ this(); this.context= context; sc = new specialcalendar(); lc = new lunarcalendar(); this.res = rs; int stepyear = year_c+jumpyear; int stepmonth = month_c+jumpmonth ; if(stepmonth > 0){ //往下一个月滑动 if(stepmonth%12 == 0){ stepyear = year_c + stepmonth/12 -1; stepmonth = 12; }else{ stepyear = year_c + stepmonth/12; stepmonth = stepmonth%12; } }else{ //往上一个月滑动 stepyear = year_c - 1 + stepmonth/12; stepmonth = stepmonth%12 + 12; if(stepmonth%12 == 0){ } } currentyear = string.valueof(stepyear);; //得到当前的年份 currentmonth = string.valueof(stepmonth); //得到本月 (jumpmonth为滑动的次数,每滑动一次就增加一月或减一月) currentday = string.valueof(day_c); //得到当前日期是哪天 getcalendar(integer.parseint(currentyear),integer.parseint(currentmonth)); } public calendaradapter(context context,resources rs,int year, int month, int day){ this(); this.context= context; sc = new specialcalendar(); lc = new lunarcalendar(); this.res = rs; currentyear = string.valueof(year);; //得到跳转到的年份 currentmonth = string.valueof(month); //得到跳转到的月份 currentday = string.valueof(day); //得到跳转到的天 getcalendar(integer.parseint(currentyear),integer.parseint(currentmonth)); } @override public int getcount() { // todo auto-generated method stub return daynumber.length; } @override public object getitem(int position) { // todo auto-generated method stub return position; } @override public long getitemid(int position) { // todo auto-generated method stub return position; } @override public view getview(int position, view convertview, viewgroup parent) { if(convertview == null){ convertview = layoutinflater.from(context).inflate(r.layout.calendar_item, null); } textview textview = (textview) convertview.findviewbyid(r.id.tvtext); string d = daynumber[position].split("\\.")[0]; string dv = daynumber[position].split("\\.")[1]; spannablestring sp = new spannablestring(d+"\n"+dv); sp.setspan(new stylespan(android.graphics.typeface.bold), 0, d.length(), spanned.span_exclusive_exclusive); sp.setspan(new relativesizespan(1.2f) , 0, d.length(), spanned.span_exclusive_exclusive); if(dv != null ||dv != ""){ sp.setspan(new relativesizespan(0.75f), d.length()+1, daynumber[position].length(), spanned.span_exclusive_exclusive); } // sp.setspan(new foregroundcolorspan(color.magenta), 14, 16, spanned.span_exclusive_exclusive) textview.settext(sp); textview.settextcolor(color.gray); // if(position<7){ // //设置周 // textview.settextcolor(color.white); // textview.setbackgroundcolor(color.search_txt_color); // textview.settextsize(14); // } if (position < daysofmonth + dayofweek && position >= dayofweek) { // 当前月信息显示 textview.settextcolor(color.black);// 当月字体设黑 drawable = res.getdrawable(r.drawable.current_day_bgc); } if(schdatetagflag != null && schdatetagflag.length >0){ for(int i = 0; i < schdatetagflag.length; i++){ if(schdatetagflag[i] == position){ //设置日程标记背景 textview.setbackgroundresource(r.drawable.mark); } } } if(currentflag == position){ //设置当天的背景 drawable = res.getdrawable(r.drawable.current_day_bgc); textview.setbackgrounddrawable(drawable); textview.settextcolor(color.white); } return convertview; } //得到某年的某月的天数且这月的第一天是星期几 public void getcalendar(int year, int month){ isleapyear = sc.isleapyear(year); //是否为闰年 daysofmonth = sc.getdaysofmonth(isleapyear, month); //某月的总天数 dayofweek = sc.getweekdayofmonth(year, month); //某月第一天为星期几 lastdaysofmonth = sc.getdaysofmonth(isleapyear, month-1); //上一个月的总天数 log.d("day", isleapyear+" ====== "+daysofmonth+" ============ "+dayofweek+" ========= "+lastdaysofmonth); getweek(year,month); } //将一个月中的每一天的值添加入数组daynuber中 private void getweek(int year, int month) { int j = 1; int flag = 0; string lunarday = ""; //得到当前月的所有日程日期(这些日期需要标记) for (int i = 0; i < daynumber.length; i++) { // 周一 // if(i<7){ // daynumber[i]=week[i]+"."+" "; // } if(i < dayofweek){ //前一个月 int temp = lastdaysofmonth - dayofweek+1; lunarday = lc.getlunardate(year, month-1, temp+i,false); daynumber[i] = (temp + i)+"."+lunarday; }else if(i < daysofmonth + dayofweek){ //本月 string day = string.valueof(i-dayofweek+1); //得到的日期 lunarday = lc.getlunardate(year, month, i-dayofweek+1,false); daynumber[i] = i-dayofweek+1+"."+lunarday; //对于当前月才去标记当前日期 if(sys_year.equals(string.valueof(year)) && sys_month.equals(string.valueof(month)) && sys_day.equals(day)){ //标记当前日期 currentflag = i; } setshowyear(string.valueof(year)); setshowmonth(string.valueof(month)); setanimalsyear(lc.animalsyear(year)); setleapmonth(lc.leapmonth == 0?"":string.valueof(lc.leapmonth)); setcyclical(lc.cyclical(year)); }else{ //下一个月 lunarday = lc.getlunardate(year, month+1, j,false); daynumber[i] = j+"."+lunarday; j++; } } string abc = ""; for(int i = 0; i < daynumber.length; i++){ abc = abc+daynumber[i]+":"; } log.d("daynumber",abc); } public void matchscheduledate(int year, int month, int day){ } /** * 点击每一个item时返回item中的日期 * @param position * @return */ public string getdatebyclickitem(int position){ return daynumber[position]; } /** * 在点击gridview时,得到这个月中第一天的位置 * @return */ public int getstartpositon(){ return dayofweek+7; } /** * 在点击gridview时,得到这个月中最后一天的位置 * @return */ public int getendposition(){ return (dayofweek+daysofmonth+7)-1; } public string getshowyear() { return showyear; } public void setshowyear(string showyear) { this.showyear = showyear; } public string getshowmonth() { return showmonth; } public void setshowmonth(string showmonth) { this.showmonth = showmonth; } public string getanimalsyear() { return animalsyear; } public void setanimalsyear(string animalsyear) { this.animalsyear = animalsyear; } public string getleapmonth() { return leapmonth; } public void setleapmonth(string leapmonth) { this.leapmonth = leapmonth; } public string getcyclical() { return cyclical; } public void setcyclical(string cyclical) { this.cyclical = cyclical; } }
计算类
import java.util.calendar; public class specialcalendar { private int daysofmonth = 0; //某月的天数 private int dayofweek = 0; //具体某一天是星期几 // 判断是否为闰年 public boolean isleapyear(int year) { if (year % 100 == 0 && year % 400 == 0) { return true; } else if (year % 100 != 0 && year % 4 == 0) { return true; } return false; } //得到某月有多少天数 public int getdaysofmonth(boolean isleapyear, int month) { switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: daysofmonth = 31; break; case 4: case 6: case 9: case 11: daysofmonth = 30; break; case 2: if (isleapyear) { daysofmonth = 29; } else { daysofmonth = 28; } } return daysofmonth; } //指定某年中的某月的第一天是星期几 public int getweekdayofmonth(int year, int month){ calendar cal = calendar.getinstance(); cal.set(year, month-1, 1); dayofweek = cal.get(calendar.day_of_week)-1; return dayofweek; } }
节日和阴历计算类
关于日历实现代码里0x04bd8, 0x04ae0, 0x0a570的解释:
new array( 0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2, 0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977, 0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970, 0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950, 0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557, 0x06ca0,0x0b550,0x15355,0x04da0,0x0a5b0,0x14573,0x052b0,0x0a9a8,0x0e950,0x06aa0, 0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0, 0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b6a0,0x195a6, 0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570, 0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0x0ab60,0x096d5,0x092e0, 0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0x0abb7,0x025d0,0x092d0,0x0cab5, 0x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0,0x0a930, 0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530, 0x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520,0x0dd45, 0x0b5a0,0x056d0,0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20,0x0ada0, 0x14b63);
正确的解释是:
二进制形式
xxxx xxxx xxxx xxxx xxxx
20-17 16-12 12-9 8-5 4-1
1-4: 表示当年有无闰年,有的话,为闰月的月份,没有的话,为0。
5-16:为除了闰月外的正常月份是大月还是小月,1为30天,0为29天。
注意:从1月到12月对应的是第16位到第5位。
17-20: 表示闰月是大月还是小月,仅当存在闰月的情况下有意义。
举个例子:
1980年的数据是: 0x095b0
二进制:0000 1001 0101 1011 0000
表示1980年没有闰月,从1月到12月的天数依次为:30、29、29、30 、29、30、29、30、 30、29、30、30。
1982年的数据是:0x0a974
0000 1010 0 1001 0111 0100
表示1982年的4月为闰月,即有第二个4月,且是闰小月。
从1月到13月的天数依次为:30、29、30、29、 29(闰月)、 30、29、29、30、 29、30、30、30。
public class lunarcalendar { private int year; //农历的年份 private int month; private int day; private string lunarmonth; //农历的月份 private boolean leap; public int leapmonth = 0; //闰的是哪个月 final static string chinesenumber[] = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" }; static simpledateformat chinesedateformat = new simpledateformat( "yyyy年mm月dd日"); final static long[] lunarinfo = new long[] { 0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6, 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, 0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0 }; //农历部分假日 final static string[] lunarholiday = new string[]{ "0101 春节", "0115 元宵", "0505 端午", "0707 七夕情人", "0715 中元", "0815 中秋", "0909 重阳", "1208 腊八", "1224 小年", "0100 除夕" }; //公历部分节假日 final static string[] solarholiday = new string[]{ "0101 元旦", "0214 情人", "0308 妇女", "0312 植树", "0315 消费者权益日", "0401 愚人", "0501 劳动", "0504 青年", "0512 护士", "0601 儿童", "0701 建党", "0801 建军", "0808 父亲", "0909 *逝世纪念", "0910 教师", "0928 孔子诞辰", "1001 国庆", "1006 老人", "1024 联合国日", "1112 孙中山诞辰纪念", "1220 澳门回归纪念", "1225 圣诞", "1226 *诞辰纪念" }; // ====== 传回农历 y年的总天数 final private static int yeardays(int y) { int i, sum = 348; for (i = 0x8000; i > 0x8; i >>= 1) { if ((lunarinfo[y - 1900] & i) != 0) sum += 1; } return (sum + leapdays(y)); } // ====== 传回农历 y年闰月的天数 final private static int leapdays(int y) { if (leapmonth(y) != 0) { if ((lunarinfo[y - 1900] & 0x10000) != 0) return 30; else return 29; } else return 0; } // ====== 传回农历 y年闰哪个月 1-12 , 没闰传回 0 final private static int leapmonth(int y) { return (int) (lunarinfo[y - 1900] & 0xf); } // ====== 传回农历 y年m月的总天数 final private static int monthdays(int y, int m) { if ((lunarinfo[y - 1900] & (0x10000 >> m)) == 0) return 29; else return 30; } // ====== 传回农历 y年的生肖 final public string animalsyear(int year) { final string[] animals = new string[] { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" }; return animals[(year - 4) % 12]; } // ====== 传入 月日的offset 传回干支, 0=甲子 final private static string cyclicalm(int num) { final string[] gan = new string[] { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" }; final string[] zhi = new string[] { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" }; return (gan[num % 10] + zhi[num % 12]); } // ====== 传入 offset 传回干支, 0=甲子 final public string cyclical(int year) { int num = year - 1900 + 36; return (cyclicalm(num)); } public static string getchinadaystring(int day) { string chineseten[] = { "初", "十", "廿", "卅" }; int n = day % 10 == 0 ? 9 : day % 10 - 1; if (day > 30) return ""; if (day == 10) return "初十"; else return chineseten[day / 10] + chinesenumber[n]; } /** */ /** * 传出y年m月d日对应的农历. yearcyl3:农历年与1864的相差数 ? moncyl4:从1900年1月31日以来,闰月数 * daycyl5:与1900年1月31日相差的天数,再加40 ? * * isday: 这个参数为false---日期为节假日时,阴历日期就返回节假日 ,true---不管日期是否为节假日依然返回这天对应的阴历日期 * @param cal * @return */ public string getlunardate(int year_log, int month_log, int day_log, boolean isday) { // @suppresswarnings("unused") int yearcyl, moncyl, daycyl; //int leapmonth = 0; string nowadays; date basedate = null; date nowaday = null; try { basedate = chinesedateformat.parse("1900年1月31日"); } catch (parseexception e) { e.printstacktrace(); // to change body of catch statement use // options | file templates. } nowadays = year_log + "年" + month_log + "月" + day_log + "日"; try { nowaday = chinesedateformat.parse(nowadays); } catch (parseexception e) { e.printstacktrace(); // to change body of catch statement use // options | file templates. } // 求出和1900年1月31日相差的天数 int offset = (int) ((nowaday.gettime() - basedate.gettime()) / 86400000l); daycyl = offset + 40; moncyl = 14; // 用offset减去每农历年的天数 // 计算当天是农历第几天 // i最终结果是农历的年份 // offset是当年的第几天 int iyear, daysofyear = 0; for (iyear = 1900; iyear < 10000 && offset > 0; iyear++) { daysofyear = yeardays(iyear); offset -= daysofyear; moncyl += 12; } if (offset < 0) { offset += daysofyear; iyear--; moncyl -= 12; } // 农历年份 year = iyear; setyear(year); //设置公历对应的农历年份 yearcyl = iyear - 1864; leapmonth = leapmonth(iyear); // 闰哪个月,1-12 leap = false; // 用当年的天数offset,逐个减去每月(农历)的天数,求出当天是本月的第几天 int imonth, daysofmonth = 0; for (imonth = 1; imonth < 13 && offset > 0; imonth++) { // 闰月 if (leapmonth > 0 && imonth == (leapmonth + 1) && !leap) { --imonth; leap = true; daysofmonth = leapdays(year); } else daysofmonth = monthdays(year, imonth); offset -= daysofmonth; // 解除闰月 if (leap && imonth == (leapmonth + 1)) leap = false; if (!leap) moncyl++; } // offset为0时,并且刚才计算的月份是闰月,要校正 if (offset == 0 && leapmonth > 0 && imonth == leapmonth + 1) { if (leap) { leap = false; } else { leap = true; --imonth; --moncyl; } } // offset小于0时,也要校正 if (offset < 0) { offset += daysofmonth; --imonth; --moncyl; } month = imonth; setlunarmonth(chinesenumber[month - 1] + "月"); //设置对应的阴历月份 day = offset + 1; if(!isday){ //如果日期为节假日则阴历日期则返回节假日 //setleapmonth(leapmonth); for(int i = 0; i < solarholiday.length; i++){ //返回公历节假日名称 string sd = solarholiday[i].split(" ")[0]; //节假日的日期 string sdv = solarholiday[i].split(" ")[1]; //节假日的名称 string smonth_v = month_log+""; string sday_v = day_log+""; string smd = ""; if(month_log < 10){ smonth_v = "0"+month_log; } if(day_log < 10){ sday_v = "0"+day_log; } smd = smonth_v+sday_v; if(sd.trim().equals(smd.trim())){ return sdv; } } for(int i = 0; i < lunarholiday.length; i++){ //返回农历节假日名称 string ld =lunarholiday[i].split(" ")[0]; //节假日的日期 string ldv = lunarholiday[i].split(" ")[1]; //节假日的名称 string lmonth_v = month+""; string lday_v = day+""; string lmd = ""; if(month < 10){ lmonth_v = "0"+month; } if(day < 10){ lday_v = "0"+day; } lmd = lmonth_v+lday_v; if(ld.trim().equals(lmd.trim())){ return ldv; } } } if (day == 1) return chinesenumber[month - 1] + "月"; else return getchinadaystring(day); } public string tostring() { if (chinesenumber[month - 1] == "一" && getchinadaystring(day) == "初一") return "农历" + year + "年"; else if (getchinadaystring(day) == "初一") return chinesenumber[month - 1] + "月"; else return getchinadaystring(day); // return year + "年" + (leap ? "闰" : "") + chinesenumber[month - 1] + // "月" + getchinadaystring(day); } /*public static void main(string[] args) { system.out.println(new lunarcalendar().getlunardate(2012, 1, 23)); }*/ public int getleapmonth() { return leapmonth; } public void setleapmonth(int leapmonth) { this.leapmonth = leapmonth; } /** * 得到当前日期对应的阴历月份 * @return */ public string getlunarmonth() { return lunarmonth; } public void setlunarmonth(string lunarmonth) { this.lunarmonth = lunarmonth; } /** * 得到当前年对应的农历年份 * @return */ public int getyear() { return year; } public void setyear(int year) { this.year = year; } }
activity
/** * 日历显示activity * * */ public class calendaractivity extends activity implements ongesturelistener { private gesturedetector gesturedetector = null; private calendaradapter calv = null; private gridview gridview = null; private textview toptext = null; private static int jumpmonth = 0; //每次滑动,增加或减去一个月,默认为0(即显示当前月) private static int jumpyear = 0; //滑动跨越一年,则增加或者减去一年,默认为0(即当前年) private int year_c = 0; private int month_c = 0; private int day_c = 0; private string currentdate = ""; private bundle bd=null;//发送参数 private bundle bun=null;//接收参数 private string ruzhutime; private string lidiantime; private string state=""; public calendaractivity() { date date = new date(); simpledateformat sdf = new simpledateformat("yyyy-m-d"); currentdate = sdf.format(date); //当期日期 year_c = integer.parseint(currentdate.split("-")[0]); month_c = integer.parseint(currentdate.split("-")[1]); day_c = integer.parseint(currentdate.split("-")[2]); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); activitylist.activitylist.add(this); setcontentview(r.layout.calendar); bd=new bundle();//out bun=getintent().getextras();//in if(bun!=null&&bun.getstring("state").equals("ruzhu")) { state=bun.getstring("state"); system.out.println("%%%%%%"+state); }else if(bun!=null&&bun.getstring("state").equals("lidian")){ state=bun.getstring("state"); system.out.println("|||||||||||"+state); } gesturedetector = new gesturedetector(this); // bd=new bundle(); calv = new calendaradapter(this,getresources(),jumpmonth,jumpyear,year_c,month_c,day_c); addgridview(); gridview.setadapter(calv); toptext = (textview) findviewbyid(r.id.tv_month); addtexttotoptextview(toptext); } @override public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { int gvflag = 0; //每次添加gridview到viewflipper中时给的标记 if (e1.getx() - e2.getx() > 120) { //像左滑动 addgridview(); //添加一个gridview jumpmonth++; //下一个月 calv = new calendaradapter(this,getresources(),jumpmonth,jumpyear,year_c,month_c,day_c); gridview.setadapter(calv); addtexttotoptextview(toptext); gvflag++; return true; } else if (e1.getx() - e2.getx() < -120) { //向右滑动 addgridview(); //添加一个gridview jumpmonth--; //上一个月 calv = new calendaradapter(this,getresources(),jumpmonth,jumpyear,year_c,month_c,day_c); gridview.setadapter(calv); gvflag++; addtexttotoptextview(toptext); return true; } return false; } /** * 创建菜单 */ @override public boolean oncreateoptionsmenu(menu menu) { menu.add(0, menu.first, menu.first, "今天"); return super.oncreateoptionsmenu(menu); } /** * 选择菜单 */ @override public boolean onmenuitemselected(int featureid, menuitem item) { switch (item.getitemid()){ case menu.first: //跳转到今天 int xmonth = jumpmonth; int xyear = jumpyear; int gvflag =0; jumpmonth = 0; jumpyear = 0; addgridview(); //添加一个gridview year_c = integer.parseint(currentdate.split("-")[0]); month_c = integer.parseint(currentdate.split("-")[1]); day_c = integer.parseint(currentdate.split("-")[2]); calv = new calendaradapter(this,getresources(),jumpmonth,jumpyear,year_c,month_c,day_c); gridview.setadapter(calv); addtexttotoptextview(toptext); gvflag++; break; } return super.onmenuitemselected(featureid, item); } @override public boolean ontouchevent(motionevent event) { return this.gesturedetector.ontouchevent(event); } @override public boolean ondown(motionevent e) { // todo auto-generated method stub return false; } @override public void onlongpress(motionevent e) { // todo auto-generated method stub } @override public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey) { // todo auto-generated method stub return false; } @override public void onshowpress(motionevent e) { // todo auto-generated method stub } @override public boolean onsingletapup(motionevent e) { // todo auto-generated method stub return false; } //添加头部的年份 闰哪月等信息 public void addtexttotoptextview(textview view){ stringbuffer textdate = new stringbuffer(); textdate.append(calv.getshowyear()).append("年").append( calv.getshowmonth()).append("月").append("\t"); view.settext(textdate); view.settextcolor(color.white); view.settypeface(typeface.default_bold); } //添加gridview private void addgridview() { gridview =(gridview)findviewbyid(r.id.gridview); gridview.setontouchlistener(new ontouchlistener() { //将gridview中的触摸事件回传给gesturedetector @override public boolean ontouch(view v, motionevent event) { // todo auto-generated method stub return calendaractivity.this.gesturedetector.ontouchevent(event); } }); gridview.setonitemclicklistener(new onitemclicklistener() { //gridview中的每一个item的点击事件 @override public void onitemclick(adapterview<?> arg0, view arg1, int position, long arg3) { //点击任何一个item,得到这个item的日期(排除点击的是周日到周六(点击不响应)) int startposition = calv.getstartpositon(); int endposition = calv.getendposition(); if(startposition <= position+7 && position <= endposition-7){ string scheduleday = calv.getdatebyclickitem(position).split("\\.")[0]; //这一天的阳历 //string schedulelunarday = calv.getdatebyclickitem(position).split("\\.")[1]; //这一天的阴历 string scheduleyear = calv.getshowyear(); string schedulemonth = calv.getshowmonth(); // toast.maketext(calendaractivity.this, scheduleyear+"-"+schedulemonth+"-"+scheduleday, 2000).show(); ruzhutime=schedulemonth+"月"+scheduleday+"日"; lidiantime=schedulemonth+"月"+scheduleday+"日"; intent intent=new intent(); if(state.equals("ruzhu")) { bd.putstring("ruzhu", ruzhutime); system.out.println("ruzhuuuuuu"+bd.getstring("ruzhu")); }else if(state.equals("lidian")){ bd.putstring("lidian", lidiantime); } intent.setclass(calendaractivity.this, hotelactivity.class); intent.putextras(bd); startactivity(intent); finish(); } } }); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 详解Android客户端与服务器交互方式
下一篇: Oracle经常用到的一些函数
推荐阅读
-
Android使用GridView实现日历功能示例(详细代码)
-
Android编程使用android-support-design实现MD风格对话框功能示例
-
Android编程使用android-support-design实现MD风格对话框功能示例
-
Android使用GridView实现日历的方法
-
Android 使用fast-verification实现验证码填写功能的实例代码
-
使用react实现手机号的数据同步显示功能的示例代码
-
Android 使用 Scroller 实现平滑滚动功能的示例代码
-
Android实现强制下线功能的示例代码
-
Android 自定义LineLayout实现满屏任意拖动功能的示例代码
-
android中GridView实现点击查看更多功能示例