Android使用GridView实现日历功能(详细代码)

转自:http://blog.csdn.net/onlyonecoder/article/details/8481799#comments

 

 

代码有点多,发个图先:



布局文件:

[html]  view plain copy
  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> 
[html]  view plain copy
  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 plain copy
  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="color: rgb(255, 0, 0); font-size: 32px;"> 
  15. </span></strong> 
[html]  view plain copy
  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:#ff00">  
  15. </span></strong>  


 


Adapter:


[java]  view plain copy
  1. /**
  2. * 日历gridview中的每一个item显示的textview
  3. * @author lmw
  4. *
  5. */ 
  6. public class CalendarAdapter extends BaseAdapter { 
  7.     private boolean isLeapyear = false;  //是否为闰年 
  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中的日期存入此数组中 
  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 = "";   //天干地支 
  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.     } 
[java]  view plain copy
  1. /** 
  2.  * 日历gridview中的每一个item显示的textview 
  3.  * @author lmw 
  4.  * 
  5.  */  
  6. public class CalendarAdapter extends BaseAdapter {  
  7.     private boolean isLeapyear = false;  //是否为闰年  
  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中的日期存入此数组中  
  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 = "";   //天干地支  
  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 plain copy
  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.      
[java]  view plain copy
  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 plain copy
  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.     } 
[java]  view plain copy
  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 plain copy
  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.      
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值