Android使用GridView實現日曆功能(詳細程式碼)

yangxi_001發表於2014-10-21

轉自:http://blog.csdn.net/onlyonecoder/article/details/8481799

程式碼有點多,發個圖先:

如果懶得往下看的,可以直接下載原始碼吧(0分的),最近一直有人要,由於時間太久了,懶得找出來整理,今天又看到有人要,正好沒事就整理了一下

http://download.csdn.net/detail/onlyonecoder/6574379


佈局檔案:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:id="@+id/lay_title"  
  9.         style="@style/TitleStyle"  
  10.         android:layout_width="fill_parent" >  
  11.   
  12.         <TextView  
  13.             android:id="@+id/btn_back"  
  14.             style="@style/TitleLeftButtonStyle" />  
  15.   
  16.         <TextView  
  17.             android:id="@+id/tv_top"  
  18.             style="@style/TitleMiddleTextStyle"  
  19.             android:text="請選擇日期" />  
  20.   
  21.         <TextView  
  22.             android:id="@+id/btn_goback_to_today"  
  23.             style="@style/TitleRightButtonStyle"  
  24.             android:text="今天" />  
  25.     </LinearLayout>  
  26.   
  27.     <LinearLayout  
  28.         android:layout_width="fill_parent"  
  29.         android:layout_height="53.0dip"  
  30.         android:background="#ff394147"  
  31.         android:gravity="center_vertical" >  
  32.   
  33.         <LinearLayout  
  34.             android:id="@+id/btn_prev_month"  
  35.             android:layout_width="0.0dip"  
  36.             android:layout_height="fill_parent"  
  37.             android:layout_weight="0.2"  
  38.             android:gravity="center"  
  39.             android:orientation="vertical" >  
  40.   
  41.             <TextView  
  42.                 android:id="@+id/left_img"  
  43.                 android:layout_width="wrap_content"  
  44.                 android:layout_height="wrap_content"  
  45.                 android:background="@drawable/triangle06_states"  
  46.                 android:duplicateParentState="true"  
  47.                 android:text=""  
  48.                 android:textSize="16.0dip" />  
  49.         </LinearLayout>  
  50.   
  51.         <LinearLayout  
  52.             android:layout_width="0.0dip"  
  53.             android:layout_height="fill_parent"  
  54.             android:layout_weight="0.6"  
  55.             android:gravity="center"  
  56.             android:orientation="vertical" >  
  57.   
  58.             <TextView  
  59.                 android:id="@+id/tv_month"  
  60.                 android:layout_width="wrap_content"  
  61.                 android:layout_height="wrap_content"  
  62.                 android:gravity="center"  
  63.                 android:text="xxxx年14月"  
  64.                 android:textColor="#ffffffff"  
  65.                 android:textSize="21.0dip" />  
  66.         </LinearLayout>  
  67.   
  68.         <LinearLayout  
  69.             android:id="@+id/btn_next_month"  
  70.             android:layout_width="0.0dip"  
  71.             android:layout_height="fill_parent"  
  72.             android:layout_weight="0.2"  
  73.             android:gravity="center"  
  74.             android:orientation="vertical" >  
  75.   
  76.             <TextView  
  77.                 android:id="@+id/right_img"  
  78.                 android:layout_width="wrap_content"  
  79.                 android:layout_height="wrap_content"  
  80.                 android:background="@drawable/triangle05_states"  
  81.                 android:duplicateParentState="true"  
  82.                 android:text=""  
  83.                 android:textSize="16.0dip" />  
  84.         </LinearLayout>  
  85.     </LinearLayout>  
  86.  <LinearLayout  
  87.         android:layout_width="fill_parent"  
  88.         android:layout_height="22.0dip"  
  89.         android:background="#ff8091a8" >  
  90.   
  91.         <TextView  
  92.             android:layout_width="0.0dip"  
  93.             android:layout_height="wrap_content"  
  94.             android:layout_gravity="center"  
  95.             android:layout_weight="1.0"  
  96.             android:gravity="center"  
  97.             android:text="週日"  
  98.             android:textColor="#ffffffff"  
  99.             android:textSize="14.0dip" />  
  100.   
  101.         <TextView  
  102.             android:layout_width="0.0dip"  
  103.             android:layout_height="wrap_content"  
  104.             android:layout_gravity="center"  
  105.             android:layout_weight="1.0"  
  106.             android:gravity="center"  
  107.             android:text="週一"  
  108.             android:textColor="#ffffffff"  
  109.             android:textSize="14.0dip" />  
  110.   
  111.         <TextView  
  112.             android:layout_width="0.0dip"  
  113.             android:layout_height="wrap_content"  
  114.             android:layout_gravity="center"  
  115.             android:layout_weight="1.0"  
  116.             android:gravity="center"  
  117.             android:text="週二"  
  118.             android:textColor="#ffffffff"  
  119.             android:textSize="14.0dip" />  
  120.   
  121.         <TextView  
  122.             android:layout_width="0.0dip"  
  123.             android:layout_height="wrap_content"  
  124.             android:layout_gravity="center"  
  125.             android:layout_weight="1.0"  
  126.             android:gravity="center"  
  127.             android:text="週三"  
  128.             android:textColor="#ffffffff"  
  129.             android:textSize="14.0dip" />  
  130.   
  131.         <TextView  
  132.             android:layout_width="0.0dip"  
  133.             android:layout_height="wrap_content"  
  134.             android:layout_gravity="center"  
  135.             android:layout_weight="1.0"  
  136.             android:gravity="center"  
  137.             android:text="週四"  
  138.             android:textColor="#ffffffff"  
  139.             android:textSize="14.0dip" />  
  140.   
  141.         <TextView  
  142.             android:layout_width="0.0dip"  
  143.             android:layout_height="wrap_content"  
  144.             android:layout_gravity="center"  
  145.             android:layout_weight="1.0"  
  146.             android:gravity="center"  
  147.             android:text="週五"  
  148.             android:textColor="#ffffffff"  
  149.             android:textSize="14.0dip" />  
  150.   
  151.         <TextView  
  152.             android:layout_width="0.0dip"  
  153.             android:layout_height="wrap_content"  
  154.             android:layout_gravity="center"  
  155.             android:layout_weight="1.0"  
  156.             android:gravity="center"  
  157.             android:text="週六"  
  158.             android:textColor="#ffffffff"  
  159.             android:textSize="14.0dip" />  
  160.     </LinearLayout>  
  161.     <GridView  
  162.         android:id="@+id/gridview"  
  163.         android:layout_width="fill_parent"  
  164.         android:layout_height="wrap_content"  
  165.         android:background="#00000000"  
  166.         android:clickable="true"  
  167.         android:clipChildren="true"  
  168.         android:columnWidth="55.0dip"  
  169.         android:horizontalSpacing="1.0dip"  
  170.         android:listSelector="@null"  
  171.         android:numColumns="7"  
  172.         android:paddingBottom="1.0dip"  
  173.         android:stretchMode="columnWidth"  
  174.         android:verticalSpacing="1.0dip" />  
  175.   
  176. </LinearLayout>  
Item:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:background="#FFFFFF"  
  5.   android:layout_width="fill_parent"  
  6.   android:layout_height="fill_parent">  
  7.     
  8.   <TextView   
  9.       android:gravity="center"  
  10.       android:layout_width="46dp"  
  11.       android:layout_height="55dp"  
  12.       android:id="@+id/tvtext"  
  13.       />  
  14. </LinearLayout><strong><span style="font-size:32px;color:#ff0000;">  
  15. </span></strong>  


Adapter:


[java] view plaincopy
  1. /** 
  2.  * 日曆<a target="_blank" style="color: #0000F0; display:inline; position:static; background:none;" href="http://www.so.com/s?q=gridview&ie=utf-8&src=se_lighten_f">gridview</a>中的每一個item顯示的textview 
  3.  * @author lmw 
  4.  * 
  5.  */  
  6. public class CalendarAdapter extends BaseAdapter {  
  7.     private boolean isLeapyear = false;  //是否為<a target="_blank" style="color: #0000F0; display:inline; position:static; background:none;" href="http://www.so.com/s?q=%E9%97%B0%E5%B9%B4&ie=utf-8&src=se_lighten_f">閏年</a>  
  8.     private int daysOfMonth = 0;      //某月的天數  
  9.     private int dayOfWeek = 0;        //具體某一天是星期幾  
  10.     private int lastDaysOfMonth = 0;  //上一個月的總天數  
  11.     private Context context;  
  12.     private String[] dayNumber = new String[42];  //一個gridview中的日期存入此<a target="_blank" style="color: #0000F0; display:inline; position:static; background:none;" href="http://www.so.com/s?q=%E6%95%B0%E7%BB%84&ie=utf-8&src=se_lighten_f">陣列</a>中  
  13. //  private static String week[] = {"週日","週一","週二","週三","週四","週五","週六"};  
  14.     private SpecialCalendar sc = null;  
  15.     private LunarCalendar lc = null;   
  16.     private Resources res = null;  
  17.     private Drawable drawable = null;  
  18.       
  19.     private String currentYear = "";  
  20.     private String currentMonth = "";  
  21.     private String currentDay = "";  
  22.       
  23.     private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");  
  24.     private int currentFlag = -1;     //用於標記當天  
  25.     private int[] schDateTagFlag = null;  //儲存當月所有的日程日期  
  26.       
  27.     private String showYear = "";   //用於在頭部顯示的年份  
  28.     private String showMonth = "";  //用於在頭部顯示的月份  
  29.     private String animalsYear = "";   
  30.     private String leapMonth = "";   //閏哪一個月  
  31.     private String cyclical = "";   //<a target="_blank" style="color: #0000F0; display:inline; position:static; background:none;" href="http://www.so.com/s?q=%E5%A4%A9%E5%B9%B2%E5%9C%B0%E6%94%AF&ie=utf-8&src=se_lighten_f">天干地支</a>  
  32.     //系統當前時間  
  33.     private String sysDate = "";    
  34.     private String sys_year = "";  
  35.     private String sys_month = "";  
  36.     private String sys_day = "";  
  37.       
  38.     public CalendarAdapter(){  
  39.         Date date = new Date();  
  40.         sysDate = sdf.format(date);  //當期日期  
  41.         sys_year = sysDate.split("-")[0];  
  42.         sys_month = sysDate.split("-")[1];  
  43.         sys_day = sysDate.split("-")[2];  
  44.           
  45.     }  
  46.       
  47.     public CalendarAdapter(Context context,Resources rs,int jumpMonth,int jumpYear,int year_c,int month_c,int day_c){  
  48.         this();  
  49.         this.context= context;  
  50.         sc = new SpecialCalendar();  
  51.         lc = new LunarCalendar();  
  52.         this.res = rs;  
  53.           
  54.         int stepYear = year_c+jumpYear;  
  55.         int stepMonth = month_c+jumpMonth ;  
  56.         if(stepMonth > 0){  
  57.             //往下一個月滑動  
  58.             if(stepMonth%12 == 0){  
  59.                 stepYear = year_c + stepMonth/12 -1;  
  60.                 stepMonth = 12;  
  61.             }else{  
  62.                 stepYear = year_c + stepMonth/12;  
  63.                 stepMonth = stepMonth%12;  
  64.             }  
  65.         }else{  
  66.             //往上一個月滑動  
  67.             stepYear = year_c - 1 + stepMonth/12;  
  68.             stepMonth = stepMonth%12 + 12;  
  69.             if(stepMonth%12 == 0){  
  70.                   
  71.             }  
  72.         }  
  73.       
  74.         currentYear = String.valueOf(stepYear);;  //得到當前的年份  
  75.         currentMonth = String.valueOf(stepMonth);  //得到本月 (jumpMonth為滑動的次數,每滑動一次就增加一月或減一月)  
  76.         currentDay = String.valueOf(day_c);  //得到當前日期是哪天  
  77.           
  78.         getCalendar(Integer.parseInt(currentYear),Integer.parseInt(currentMonth));  
  79.           
  80.     }  
  81.       
  82.     public CalendarAdapter(Context context,Resources rs,int year, int month, int day){  
  83.         this();  
  84.         this.context= context;  
  85.         sc = new SpecialCalendar();  
  86.         lc = new LunarCalendar();  
  87.         this.res = rs;  
  88.         currentYear = String.valueOf(year);;  //得到跳轉到的年份  
  89.         currentMonth = String.valueOf(month);  //得到跳轉到的月份  
  90.         currentDay = String.valueOf(day);  //得到跳轉到的天  
  91.           
  92.         getCalendar(Integer.parseInt(currentYear),Integer.parseInt(currentMonth));  
  93.           
  94.     }  
  95.       
  96.     @Override  
  97.     public int getCount() {  
  98.         // TODO Auto-generated method stub  
  99.         return dayNumber.length;  
  100.     }  
  101.   
  102.     @Override  
  103.     public Object getItem(int position) {  
  104.         // TODO Auto-generated method stub  
  105.         return position;  
  106.     }  
  107.   
  108.     @Override  
  109.     public long getItemId(int position) {  
  110.         // TODO Auto-generated method stub  
  111.         return position;  
  112.     }  
  113.   
  114.     @Override  
  115.     public View getView(int position, View convertView, ViewGroup parent) {  
  116.   
  117.         if(convertView == null){  
  118.             convertView = LayoutInflater.from(context).inflate(R.layout.calendar_item, null);  
  119.          }  
  120.         TextView textView = (TextView) convertView.findViewById(R.id.tvtext);  
  121.         String d = dayNumber[position].split("\\.")[0];  
  122.         String dv = dayNumber[position].split("\\.")[1];  
  123.   
  124.         SpannableString sp = new SpannableString(d+"\n"+dv);  
  125.         sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, d.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
  126.         sp.setSpan(new RelativeSizeSpan(1.2f) , 0, d.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
  127.         if(dv != null ||dv != ""){  
  128.             sp.setSpan(new RelativeSizeSpan(0.75f), d.length()+1, dayNumber[position].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
  129.         }  
  130. //      sp.setSpan(new ForegroundColorSpan(Color.MAGENTA), 14, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)  
  131.         textView.setText(sp);  
  132.         textView.setTextColor(Color.GRAY);  
  133.           
  134. //      if(position<7){  
  135. //          //設定周  
  136. //          textView.setTextColor(Color.WHITE);  
  137. //          textView.setBackgroundColor(color.search_txt_color);  
  138. //          textView.setTextSize(14);  
  139. //      }  
  140.           
  141.         if (position < daysOfMonth + dayOfWeek && position >= dayOfWeek) {  
  142.             // 當前月資訊顯示  
  143.             textView.setTextColor(Color.BLACK);// 當月字型設黑  
  144.             drawable = res.getDrawable(R.drawable.current_day_bgc);  
  145.   
  146.         }  
  147.         if(schDateTagFlag != null && schDateTagFlag.length >0){  
  148.             for(int i = 0; i < schDateTagFlag.length; i++){  
  149.                 if(schDateTagFlag[i] == position){  
  150.                     //設定日程標記背景  
  151.                     textView.setBackgroundResource(R.drawable.mark);  
  152.                 }  
  153.             }  
  154.         }  
  155.         if(currentFlag == position){   
  156.             //設定當天的背景  
  157.             drawable = res.getDrawable(R.drawable.current_day_bgc);  
  158.             textView.setBackgroundDrawable(drawable);  
  159.             textView.setTextColor(Color.WHITE);  
  160.         }  
  161.         return convertView;  
  162.     }  
  163.       
  164.     //得到某年的某月的天數且這月的第一天是星期幾  
  165.     public void getCalendar(int year, int month){  
  166.         isLeapyear = sc.isLeapYear(year);              //是否為閏年  
  167.         daysOfMonth = sc.getDaysOfMonth(isLeapyear, month);  //某月的總天數  
  168.         dayOfWeek = sc.getWeekdayOfMonth(year, month);      //某月第一天為星期幾  
  169.         lastDaysOfMonth = sc.getDaysOfMonth(isLeapyear, month-1);  //上一個月的總天數  
  170.         Log.d("DAY", isLeapyear+" ======  "+daysOfMonth+"  ============  "+dayOfWeek+"  =========   "+lastDaysOfMonth);  
  171.         getweek(year,month);  
  172.     }  
  173.       
  174.     //將一個月中的每一天的值新增入陣列dayNuber中  
  175.     private void getweek(int year, int month) {  
  176.         int j = 1;  
  177.         int flag = 0;  
  178.         String lunarDay = "";  
  179.           
  180.         //得到當前月的所有日程日期(這些日期需要標記)  
  181.   
  182.         for (int i = 0; i < dayNumber.length; i++) {  
  183.             // 週一  
  184. //          if(i<7){  
  185. //              dayNumber[i]=week[i]+"."+" ";  
  186. //          }  
  187.              if(i < dayOfWeek){  //前一個月  
  188.                 int temp = lastDaysOfMonth - dayOfWeek+1;  
  189.                 lunarDay = lc.getLunarDate(year, month-1, temp+i,false);  
  190.                 dayNumber[i] = (temp + i)+"."+lunarDay;  
  191.             }else if(i < daysOfMonth + dayOfWeek){   //本月  
  192.                 String day = String.valueOf(i-dayOfWeek+1);   //得到的日期  
  193.                 lunarDay = lc.getLunarDate(year, month, i-dayOfWeek+1,false);  
  194.                 dayNumber[i] = i-dayOfWeek+1+"."+lunarDay;  
  195.                 //對於當前月才去標記當前日期  
  196.                 if(sys_year.equals(String.valueOf(year)) && sys_month.equals(String.valueOf(month)) && sys_day.equals(day)){  
  197.                     //標記當前日期  
  198.                     currentFlag = i;  
  199.                 }     
  200.                 setShowYear(String.valueOf(year));  
  201.                 setShowMonth(String.valueOf(month));  
  202.                 setAnimalsYear(lc.animalsYear(year));  
  203.                 setLeapMonth(lc.leapMonth == 0?"":String.valueOf(lc.leapMonth));  
  204.                 setCyclical(lc.cyclical(year));  
  205.             }else{   //下一個月  
  206.                 lunarDay = lc.getLunarDate(year, month+1, j,false);  
  207.                 dayNumber[i] = j+"."+lunarDay;  
  208.                 j++;  
  209.             }  
  210.         }  
  211.           
  212.         String abc = "";  
  213.         for(int i = 0; i < dayNumber.length; i++){  
  214.              abc = abc+dayNumber[i]+":";  
  215.         }  
  216.         Log.d("DAYNUMBER",abc);  
  217.   
  218.   
  219.     }  
  220.       
  221.       
  222.     public void matchScheduleDate(int year, int month, int day){  
  223.           
  224.     }  
  225.       
  226.     /** 
  227.      * 點選每一個item時返回item中的日期 
  228.      * @param position 
  229.      * @return 
  230.      */  
  231.     public String getDateByClickItem(int position){  
  232.         return dayNumber[position];  
  233.     }  
  234.       
  235.     /** 
  236.      * 在點選gridView時,得到這個月中第一天的位置 
  237.      * @return 
  238.      */  
  239.     public int getStartPositon(){  
  240.         return dayOfWeek+7;  
  241.     }  
  242.       
  243.     /** 
  244.      * 在點選gridView時,得到這個月中最後一天的位置 
  245.      * @return 
  246.      */  
  247.     public int getEndPosition(){  
  248.         return  (dayOfWeek+daysOfMonth+7)-1;  
  249.     }  
  250.       
  251.     public String getShowYear() {  
  252.         return showYear;  
  253.     }  
  254.   
  255.     public void setShowYear(String showYear) {  
  256.         this.showYear = showYear;  
  257.     }  
  258.   
  259.     public String getShowMonth() {  
  260.         return showMonth;  
  261.     }  
  262.   
  263.     public void setShowMonth(String showMonth) {  
  264.         this.showMonth = showMonth;  
  265.     }  
  266.       
  267.     public String getAnimalsYear() {  
  268.         return animalsYear;  
  269.     }  
  270.   
  271.     public void setAnimalsYear(String animalsYear) {  
  272.         this.animalsYear = animalsYear;  
  273.     }  
  274.       
  275.     public String getLeapMonth() {  
  276.         return leapMonth;  
  277.     }  
  278.   
  279.     public void setLeapMonth(String leapMonth) {  
  280.         this.leapMonth = leapMonth;  
  281.     }  
  282.       
  283.     public String getCyclical() {  
  284.         return cyclical;  
  285.     }  
  286.   
  287.     public void setCyclical(String cyclical) {  
  288.         this.cyclical = cyclical;  
  289.     }  
  290. }  


計算類

[java] view plaincopy
  1. import java.util.Calendar;  
  2.   
  3. public class SpecialCalendar {  
  4.   
  5.     private int daysOfMonth = 0;      //某月的天數  
  6.     private int dayOfWeek = 0;        //具體某一天是星期幾  
  7.   
  8.       
  9.       
  10.       
  11.     // 判斷是否為閏年  
  12.     public boolean isLeapYear(int year) {  
  13.         if (year % 100 == 0 && year % 400 == 0) {  
  14.             return true;  
  15.         } else if (year % 100 != 0 && year % 4 == 0) {  
  16.             return true;  
  17.         }  
  18.         return false;  
  19.     }  
  20.   
  21.     //得到某月有多少天數  
  22.     public int getDaysOfMonth(boolean isLeapyear, int month) {  
  23.         switch (month) {  
  24.         case 1:  
  25.         case 3:  
  26.         case 5:  
  27.         case 7:  
  28.         case 8:  
  29.         case 10:  
  30.         case 12:  
  31.             daysOfMonth = 31;  
  32.             break;  
  33.         case 4:  
  34.         case 6:  
  35.         case 9:  
  36.         case 11:  
  37.             daysOfMonth = 30;  
  38.             break;  
  39.         case 2:  
  40.             if (isLeapyear) {  
  41.                 daysOfMonth = 29;  
  42.             } else {  
  43.                 daysOfMonth = 28;  
  44.             }  
  45.   
  46.         }  
  47.         return daysOfMonth;  
  48.     }  
  49.       
  50.     //指定某年中的某月的第一天是星期幾  
  51.     public int getWeekdayOfMonth(int year, int month){  
  52.         Calendar cal = Calendar.getInstance();  
  53.         cal.set(year, month-11);  
  54.         dayOfWeek = cal.get(Calendar.DAY_OF_WEEK)-1;  
  55.         return dayOfWeek;  
  56.     }  
  57.       
  58.       
  59. }  

節日和陰曆計算類

關於日曆實現程式碼裡0x04bd8, 0x04ae0, 0x0a570的解釋:

http://blog.csdn.net/onlyonecoder/article/details/8484118

[java] view plaincopy
  1. public class LunarCalendar {  
  2.     private int year;   //農曆的年份  
  3.     private int month;  
  4.     private int day;  
  5.     private String lunarMonth;   //農曆的月份  
  6.     private boolean leap;  
  7.     public int leapMonth = 0;   //閏的是哪個月  
  8.       
  9.     final static String chineseNumber[] = { "一""二""三""四""五""六""七",  
  10.             "八""九""十""十一""十二" };  
  11.     static SimpleDateFormat chineseDateFormat = new SimpleDateFormat(  
  12.             "yyyy年MM月dd日");  
  13.     final static long[] lunarInfo = new long[] { 0x04bd80x04ae00x0a570,  
  14.             0x054d50x0d2600x0d9500x165540x056a00x09ad00x055d2,  
  15.             0x04ae00x0a5b60x0a4d00x0d2500x1d2550x0b5400x0d6a0,  
  16.             0x0ada20x095b00x149770x049700x0a4b00x0b4b50x06a50,  
  17.             0x06d400x1ab540x02b600x095700x052f20x049700x06566,  
  18.             0x0d4a00x0ea500x06e950x05ad00x02b600x186e30x092e0,  
  19.             0x1c8d70x0c9500x0d4a00x1d8a60x0b5500x056a00x1a5b4,  
  20.             0x025d00x092d00x0d2b20x0a9500x0b5570x06ca00x0b550,  
  21.             0x153550x04da00x0a5d00x145730x052d00x0a9a80x0e950,  
  22.             0x06aa00x0aea60x0ab500x04b600x0aae40x0a5700x05260,  
  23.             0x0f2630x0d9500x05b570x056a00x096d00x04dd50x04ad0,  
  24.             0x0a4d00x0d4d40x0d2500x0d5580x0b5400x0b5a00x195a6,  
  25.             0x095b00x049b00x0a9740x0a4b00x0b27a0x06a500x06d40,  
  26.             0x0af460x0ab600x095700x04af50x049700x064b00x074a3,  
  27.             0x0ea500x06b580x055c00x0ab600x096d50x092e00x0c960,  
  28.             0x0d9540x0d4a00x0da500x075520x056a00x0abb70x025d0,  
  29.             0x092d00x0cab50x0a9500x0b4a00x0baa40x0ad500x055d9,  
  30.             0x04ba00x0a5b00x151760x052b00x0a9300x079540x06aa0,  
  31.             0x0ad500x05b520x04b600x0a6e60x0a4e00x0d2600x0ea65,  
  32.             0x0d5300x05aa00x076a30x096d00x04bd70x04ad00x0a4d0,  
  33.             0x1d0b60x0d2500x0d5200x0dd450x0b5a00x056d00x055b2,  
  34.             0x049b00x0a5770x0a4b00x0aa500x1b2550x06d200x0ada0 };  
  35.   
  36.     //農曆部分假日  
  37.     final static String[] lunarHoliday = new String[]{  
  38.                                         "0101 春節",  
  39.                                         "0115 元宵",  
  40.                                         "0505 端午",  
  41.                                         "0707 七夕情人",  
  42.                                         "0715 中元",  
  43.                                         "0815 中秋",  
  44.                                         "0909 重陽",  
  45.                                         "1208 臘八",  
  46.                                         "1224 小年",  
  47.                                         "0100 除夕"  
  48.     };  
  49.       
  50.     //公曆部分節假日  
  51.     final static String[] solarHoliday = new String[]{  
  52.                                         "0101 元旦",  
  53.                                         "0214 情人",  
  54.                                         "0308 婦女",  
  55.                                         "0312 植樹",  
  56.                                         "0315 消費者權益日",  
  57.                                         "0401 愚人",  
  58.                                         "0501 勞動",  
  59.                                         "0504 青年",  
  60.                                         "0512 護士",  
  61.                                         "0601 兒童",  
  62.                                         "0701 建黨",  
  63.                                         "0801 建軍",  
  64.                                         "0808 父親",  
  65.                                         "0909 毛澤東逝世紀念",  
  66.                                         "0910 教師",  
  67.                                         "0928 孔子誕辰",  
  68.                                         "1001 國慶",  
  69.                                         "1006 老人",  
  70.                                         "1024 聯合國日",  
  71.                                         "1112 孫中山誕辰紀念",  
  72.                                         "1220 澳門迴歸紀念",  
  73.                                         "1225 聖誕",  
  74.                                         "1226 毛澤東誕辰紀念"  
  75.     };  
  76.       
  77.     // ====== 傳回農曆 y年的總天數  
  78.     final private static int yearDays(int y) {  
  79.         int i, sum = 348;  
  80.         for (i = 0x8000; i > 0x8; i >>= 1) {  
  81.             if ((lunarInfo[y - 1900] & i) != 0)  
  82.                 sum += 1;  
  83.         }  
  84.         return (sum + leapDays(y));  
  85.     }  
  86.   
  87.     // ====== 傳回農曆 y年閏月的天數  
  88.     final private static int leapDays(int y) {  
  89.         if (leapMonth(y) != 0) {  
  90.             if ((lunarInfo[y - 1900] & 0x10000) != 0)  
  91.                 return 30;  
  92.             else  
  93.                 return 29;  
  94.         } else  
  95.             return 0;  
  96.     }  
  97.   
  98.     // ====== 傳回農曆 y年閏哪個月 1-12 , 沒閏傳回 0  
  99.     final private static int leapMonth(int y) {  
  100.         return (int) (lunarInfo[y - 1900] & 0xf);  
  101.     }  
  102.   
  103.     // ====== 傳回農曆 y年m月的總天數  
  104.     final private static int monthDays(int y, int m) {  
  105.         if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0)  
  106.             return 29;  
  107.         else  
  108.             return 30;  
  109.     }  
  110.   
  111.     // ====== 傳回農曆 y年的生肖  
  112.     final public String animalsYear(int year) {  
  113.         final String[] Animals = new String[] { "鼠""牛""虎""兔""龍""蛇",  
  114.                 "馬""羊""猴""雞""狗""豬" };  
  115.         return Animals[(year - 4) % 12];  
  116.     }  
  117.   
  118.     // ====== 傳入 月日的offset 傳回干支, 0=甲子  
  119.     final private static String cyclicalm(int num) {  
  120.         final String[] Gan = new String[] { "甲""乙""丙""丁""戊""己""庚",  
  121.                 "辛""壬""癸" };  
  122.         final String[] Zhi = new String[] { "子""醜""寅""卯""辰""巳""午",  
  123.                 "未""申""酉""戌""亥" };  
  124.         return (Gan[num % 10] + Zhi[num % 12]);  
  125.     }  
  126.   
  127.     // ====== 傳入 offset 傳回干支, 0=甲子  
  128.     final public String cyclical(int year) {  
  129.         int num = year - 1900 + 36;  
  130.         return (cyclicalm(num));  
  131.     }  
  132.   
  133.     public static String getChinaDayString(int day) {  
  134.         String chineseTen[] = { "初""十""廿""卅" };  
  135.         int n = day % 10 == 0 ? 9 : day % 10 - 1;  
  136.         if (day > 30)  
  137.             return "";  
  138.         if (day == 10)  
  139.             return "初十";  
  140.         else  
  141.             return chineseTen[day / 10] + chineseNumber[n];  
  142.     }  
  143.   
  144.     /** */  
  145.     /** 
  146.      * 傳出y年m月d日對應的農曆. yearCyl3:農曆年與1864的相差數 ? monCyl4:從1900年1月31日以來,閏月數 
  147.      * dayCyl5:與1900年1月31日相差的天數,再加40 ? 
  148.      *  
  149.      * isday: 這個引數為false---日期為節假日時,陰曆日期就返回節假日 ,true---不管日期是否為節假日依然返回這天對應的陰曆日期 
  150.      * @param cal 
  151.      * @return 
  152.      */  
  153.     public String getLunarDate(int year_log, int month_log, int day_log, boolean isday) {  
  154.         // @SuppressWarnings("unused")  
  155.         int yearCyl, monCyl, dayCyl;  
  156.         //int leapMonth = 0;  
  157.         String nowadays;  
  158.         Date baseDate = null;  
  159.         Date nowaday = null;  
  160.         try {  
  161.             baseDate = chineseDateFormat.parse("1900年1月31日");  
  162.         } catch (ParseException e) {  
  163.             e.printStackTrace(); // To change body of catch statement use  
  164.             // Options | File Templates.  
  165.         }  
  166.   
  167.         nowadays = year_log + "年" + month_log + "月" + day_log + "日";  
  168.         try {  
  169.             nowaday = chineseDateFormat.parse(nowadays);  
  170.         } catch (ParseException e) {  
  171.             e.printStackTrace(); // To change body of catch statement use  
  172.             // Options | File Templates.  
  173.         }  
  174.   
  175.         // 求出和1900年1月31日相差的天數  
  176.         int offset = (int) ((nowaday.getTime() - baseDate.getTime()) / 86400000L);  
  177.         dayCyl = offset + 40;  
  178.         monCyl = 14;  
  179.   
  180.         // 用offset減去每農曆年的天數  
  181.         // 計算當天是農曆第幾天  
  182.         // i最終結果是農曆的年份  
  183.         // offset是當年的第幾天  
  184.         int iYear, daysOfYear = 0;  
  185.         for (iYear = 1900; iYear < 10000 && offset > 0; iYear++) {  
  186.             daysOfYear = yearDays(iYear);  
  187.             offset -= daysOfYear;  
  188.             monCyl += 12;  
  189.         }  
  190.         if (offset < 0) {  
  191.             offset += daysOfYear;  
  192.             iYear--;  
  193.             monCyl -= 12;  
  194.         }  
  195.         // 農曆年份  
  196.         year = iYear;  
  197.         setYear(year);  //設定公曆對應的農曆年份  
  198.           
  199.         yearCyl = iYear - 1864;  
  200.         leapMonth = leapMonth(iYear); // 閏哪個月,1-12  
  201.         leap = false;  
  202.   
  203.         // 用當年的天數offset,逐個減去每月(農曆)的天數,求出當天是本月的第幾天  
  204.         int iMonth, daysOfMonth = 0;  
  205.         for (iMonth = 1; iMonth < 13 && offset > 0; iMonth++) {  
  206.             // 閏月  
  207.             if (leapMonth > 0 && iMonth == (leapMonth + 1) && !leap) {  
  208.                 --iMonth;  
  209.                 leap = true;  
  210.                 daysOfMonth = leapDays(year);  
  211.             } else  
  212.                 daysOfMonth = monthDays(year, iMonth);  
  213.   
  214.             offset -= daysOfMonth;  
  215.             // 解除閏月  
  216.             if (leap && iMonth == (leapMonth + 1))  
  217.                 leap = false;  
  218.             if (!leap)  
  219.                 monCyl++;  
  220.         }  
  221.         // offset為0時,並且剛才計算的月份是閏月,要校正  
  222.         if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) {  
  223.             if (leap) {  
  224.                 leap = false;  
  225.             } else {  
  226.                 leap = true;  
  227.                 --iMonth;  
  228.                 --monCyl;  
  229.             }  
  230.         }  
  231.         // offset小於0時,也要校正  
  232.         if (offset < 0) {  
  233.             offset += daysOfMonth;  
  234.             --iMonth;  
  235.             --monCyl;  
  236.         }  
  237.         month = iMonth;  
  238.         setLunarMonth(chineseNumber[month - 1] + "月");  //設定對應的陰曆月份  
  239.         day = offset + 1;  
  240.   
  241.         if(!isday){  
  242.             //如果日期為節假日則陰曆日期則返回節假日  
  243.             //setLeapMonth(leapMonth);  
  244.             for(int i = 0; i < solarHoliday.length; i++){  
  245.                 //返回公曆節假日名稱  
  246.                 String sd = solarHoliday[i].split(" ")[0];  //節假日的日期  
  247.                 String sdv = solarHoliday[i].split(" ")[1]; //節假日的名稱  
  248.                 String smonth_v = month_log+"";  
  249.                 String sday_v = day_log+"";  
  250.                 String smd = "";  
  251.                 if(month_log < 10){  
  252.                     smonth_v = "0"+month_log;  
  253.                 }  
  254.                 if(day_log < 10){  
  255.                     sday_v = "0"+day_log;  
  256.                 }  
  257.                 smd = smonth_v+sday_v;  
  258.                 if(sd.trim().equals(smd.trim())){  
  259.                     return sdv;  
  260.                 }  
  261.             }  
  262.               
  263.             for(int i = 0; i < lunarHoliday.length; i++){  
  264.                 //返回農曆節假日名稱  
  265.                 String ld =lunarHoliday[i].split(" ")[0];   //節假日的日期  
  266.                 String ldv = lunarHoliday[i].split(" ")[1];  //節假日的名稱  
  267.                 String lmonth_v = month+"";  
  268.                 String lday_v = day+"";  
  269.                 String lmd = "";  
  270.                 if(month < 10){  
  271.                     lmonth_v = "0"+month;  
  272.                 }  
  273.                 if(day < 10){  
  274.                     lday_v = "0"+day;  
  275.                 }  
  276.                 lmd = lmonth_v+lday_v;  
  277.                 if(ld.trim().equals(lmd.trim())){  
  278.                     return ldv;         
  279.                 }  
  280.             }  
  281.         }  
  282.         if (day == 1)  
  283.             return chineseNumber[month - 1] + "月";  
  284.         else  
  285.             return getChinaDayString(day);  
  286.   
  287.     }  
  288.   
  289.       
  290.     public String toString() {  
  291.         if (chineseNumber[month - 1] == "一" && getChinaDayString(day) == "初一")  
  292.             return "農曆" + year + "年";  
  293.         else if (getChinaDayString(day) == "初一")  
  294.             return chineseNumber[month - 1] + "月";  
  295.         else  
  296.             return getChinaDayString(day);  
  297.         // return year + "年" + (leap ? "閏" : "") + chineseNumber[month - 1] +  
  298.         // "月" + getChinaDayString(day);  
  299.     }  
  300.       
  301.     /*public static void main(String[] args) { 
  302.         System.out.println(new LunarCalendar().getLunarDate(2012, 1, 23)); 
  303.     }*/  
  304.       
  305.     public int getLeapMonth() {  
  306.         return leapMonth;  
  307.     }  
  308.   
  309.     public void setLeapMonth(int leapMonth) {  
  310.         this.leapMonth = leapMonth;  
  311.     }  
  312.       
  313.     /** 
  314.      * 得到當前日期對應的陰曆月份 
  315.      * @return 
  316.      */  
  317.     public String getLunarMonth() {  
  318.         return lunarMonth;  
  319.     }  
  320.       
  321.     public void setLunarMonth(String lunarMonth) {  
  322.         this.lunarMonth = lunarMonth;  
  323.     }  
  324.       
  325.     /** 
  326.      * 得到當前年對應的農曆年份 
  327.      * @return 
  328.      */  
  329.     public int getYear() {  
  330.         return year;  
  331.     }  
  332.   
  333.     public void setYear(int year) {  
  334.         this.year = year;  
  335.     }  
  336. }  




Activity


[java] view plaincopy
  1. /** 
  2.  * 日曆顯示activity 
  3.  *  
  4.  * 
  5.  */  
  6. public class CalendarActivity extends Activity implements OnGestureListener {  
  7.   
  8.     private GestureDetector gestureDetector = null;  
  9.     private CalendarAdapter calV = null;  
  10.     private GridView gridView = null;  
  11.     private TextView topText = null;  
  12.     private static int jumpMonth = 0;      //每次滑動,增加或減去一個月,預設為0(即顯示當前月)  
  13.     private static int jumpYear = 0;       //滑動跨越一年,則增加或者減去一年,預設為0(即當前年)  
  14.     private int year_c = 0;  
  15.     private int month_c = 0;  
  16.     private int day_c = 0;  
  17.     private String currentDate = "";  
  18.     private Bundle bd=null;//傳送引數  
  19.     private Bundle bun=null;//接收引數  
  20.     private String ruzhuTime;  
  21.     private String lidianTime;  
  22.     private String state="";  
  23.       
  24.     public CalendarActivity() {  
  25.   
  26.         Date date = new Date();  
  27.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");  
  28.         currentDate = sdf.format(date);  //當期日期  
  29.         year_c = Integer.parseInt(currentDate.split("-")[0]);  
  30.         month_c = Integer.parseInt(currentDate.split("-")[1]);  
  31.         day_c = Integer.parseInt(currentDate.split("-")[2]);  
  32.           
  33.           
  34.     }  
  35.   
  36.     @Override  
  37.     public void onCreate(Bundle savedInstanceState) {  
  38.         super.onCreate(savedInstanceState);  
  39.         ActivityList.activityList.add(this);  
  40.         setContentView(R.layout.calendar);  
  41.         bd=new Bundle();//out  
  42.         bun=getIntent().getExtras();//in  
  43.           
  44.           
  45.           if(bun!=null&&bun.getString("state").equals("ruzhu"))  
  46.           {  
  47.             state=bun.getString("state");  
  48.             System.out.println("%%%%%%"+state);  
  49.           }else if(bun!=null&&bun.getString("state").equals("lidian")){  
  50.               
  51.             state=bun.getString("state");  
  52.             System.out.println("|||||||||||"+state);  
  53.           }  
  54.           
  55.         gestureDetector = new GestureDetector(this);  
  56. //      bd=new Bundle();  
  57.         calV = new CalendarAdapter(this,getResources(),jumpMonth,jumpYear,year_c,month_c,day_c);  
  58.         addGridView();  
  59.         gridView.setAdapter(calV);  
  60.           
  61.         topText = (TextView) findViewById(R.id.tv_month);  
  62.         addTextToTopTextView(topText);  
  63.       
  64.     }  
  65.       
  66.     @Override  
  67.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  68.             float velocityY) {  
  69.         int gvFlag = 0;         //每次新增gridview到viewflipper中時給的標記  
  70.         if (e1.getX() - e2.getX() > 120) {  
  71.             //像左滑動  
  72.             addGridView();   //新增一個gridView  
  73.             jumpMonth++;     //下一個月  
  74.               
  75.             calV = new CalendarAdapter(this,getResources(),jumpMonth,jumpYear,year_c,month_c,day_c);  
  76.             gridView.setAdapter(calV);  
  77.             addTextToTopTextView(topText);  
  78.             gvFlag++;  
  79.       
  80.             return true;  
  81.         } else if (e1.getX() - e2.getX() < -120) {  
  82.             //向右滑動  
  83.             addGridView();   //新增一個gridView  
  84.             jumpMonth--;     //上一個月  
  85.               
  86.             calV = new CalendarAdapter(this,getResources(),jumpMonth,jumpYear,year_c,month_c,day_c);  
  87.             gridView.setAdapter(calV);  
  88.             gvFlag++;  
  89.             addTextToTopTextView(topText);  
  90.   
  91.             return true;  
  92.         }  
  93.         return false;  
  94.     }  
  95.       
  96.       
  97.     /** 
  98.      * 建立選單 
  99.      */  
  100.     @Override  
  101.     public boolean onCreateOptionsMenu(Menu menu) {  
  102.   
  103.         menu.add(0, menu.FIRST, menu.FIRST, "今天");  
  104.         return super.onCreateOptionsMenu(menu);  
  105.     }  
  106.       
  107.     /** 
  108.      * 選擇選單 
  109.      */  
  110.     @Override  
  111.     public boolean onMenuItemSelected(int featureId, MenuItem item) {  
  112.         switch (item.getItemId()){  
  113.         case Menu.FIRST:  
  114.             //跳轉到今天  
  115.             int xMonth = jumpMonth;  
  116.             int xYear = jumpYear;  
  117.             int gvFlag =0;  
  118.             jumpMonth = 0;  
  119.             jumpYear = 0;  
  120.             addGridView();   //新增一個gridView  
  121.             year_c = Integer.parseInt(currentDate.split("-")[0]);  
  122.             month_c = Integer.parseInt(currentDate.split("-")[1]);  
  123.             day_c = Integer.parseInt(currentDate.split("-")[2]);  
  124.             calV = new CalendarAdapter(this,getResources(),jumpMonth,jumpYear,year_c,month_c,day_c);  
  125.             gridView.setAdapter(calV);  
  126.             addTextToTopTextView(topText);  
  127.             gvFlag++;  
  128.   
  129.             break;  
  130.         }  
  131.         return super.onMenuItemSelected(featureId, item);  
  132.     }  
  133.       
  134.     @Override  
  135.     public boolean onTouchEvent(MotionEvent event) {  
  136.   
  137.         return this.gestureDetector.onTouchEvent(event);  
  138.     }  
  139.   
  140.     @Override  
  141.     public boolean onDown(MotionEvent e) {  
  142.         // TODO Auto-generated method stub  
  143.         return false;  
  144.     }  
  145.   
  146.     @Override  
  147.     public void onLongPress(MotionEvent e) {  
  148.         // TODO Auto-generated method stub  
  149.   
  150.     }  
  151.   
  152.     @Override  
  153.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,  
  154.             float distanceY) {  
  155.         // TODO Auto-generated method stub  
  156.         return false;  
  157.     }  
  158.   
  159.     @Override  
  160.     public void onShowPress(MotionEvent e) {  
  161.         // TODO Auto-generated method stub  
  162.     }  
  163.   
  164.     @Override  
  165.     public boolean onSingleTapUp(MotionEvent e) {  
  166.         // TODO Auto-generated method stub  
  167.         return false;  
  168.     }  
  169.       
  170.     //新增頭部的年份 閏哪月等資訊  
  171.     public void addTextToTopTextView(TextView view){  
  172.         StringBuffer textDate = new StringBuffer();  
  173.         textDate.append(calV.getShowYear()).append("年").append(  
  174.                 calV.getShowMonth()).append("月").append("\t");  
  175.         view.setText(textDate);  
  176.         view.setTextColor(Color.WHITE);  
  177.         view.setTypeface(Typeface.DEFAULT_BOLD);  
  178.     }  
  179.       
  180.     //新增gridview  
  181.     private void addGridView() {  
  182.           
  183.         gridView =(GridView)findViewById(R.id.gridview);  
  184.   
  185.         gridView.setOnTouchListener(new OnTouchListener() {  
  186.             //將gridview中的觸控事件回傳給gestureDetector  
  187.             @Override  
  188.             public boolean onTouch(View v, MotionEvent event) {  
  189.                 // TODO Auto-generated method stub  
  190.                 return CalendarActivity.this.gestureDetector.onTouchEvent(event);  
  191.             }  
  192.         });             
  193.           
  194.         gridView.setOnItemClickListener(new OnItemClickListener() {  
  195.             //gridView中的每一個item的點選事件  
  196.               
  197.             @Override  
  198.             public void onItemClick(AdapterView<?> arg0, View arg1, int position,  
  199.                     long arg3) {  
  200.                   //點選任何一個item,得到這個item的日期(排除點選的是週日到週六(點選不響應))  
  201.                   int startPosition = calV.getStartPositon();  
  202.                   int endPosition = calV.getEndPosition();  
  203.                   if(startPosition <= position+7  && position <= endPosition-7){  
  204.                       String scheduleDay = calV.getDateByClickItem(position).split("\\.")[0];  //這一天的陽曆  
  205.                       //String scheduleLunarDay = calV.getDateByClickItem(position).split("\\.")[1];  //這一天的陰曆  
  206.                       String scheduleYear = calV.getShowYear();  
  207.                       String scheduleMonth = calV.getShowMonth();  
  208. //                    Toast.makeText(CalendarActivity.this, scheduleYear+"-"+scheduleMonth+"-"+scheduleDay, 2000).show();  
  209.                       ruzhuTime=scheduleMonth+"月"+scheduleDay+"日";                      
  210.                       lidianTime=scheduleMonth+"月"+scheduleDay+"日";         
  211.                     Intent intent=new Intent();  
  212.                     if(state.equals("ruzhu"))  
  213.                     {  
  214.                           
  215.                         bd.putString("ruzhu", ruzhuTime);  
  216.                         System.out.println("ruzhuuuuuu"+bd.getString("ruzhu"));  
  217.                     }else if(state.equals("lidian")){  
  218.                           
  219.                         bd.putString("lidian", lidianTime);  
  220.                     }  
  221.                     intent.setClass(CalendarActivity.this, HotelActivity.class);                   
  222.                     intent.putExtras(bd);  
  223.                     startActivity(intent);  
  224.                     finish();  
  225.                     }  
  226.                   }  
  227.               
  228.         });  
  229.     }  
  230.       
  231. }  

相關文章