Android萬年歷實現源代碼
/**
日期工具類 / public class DateUtils {
/** * 某月的天數 */ private static int daysOfMonth = 0; /** * 具體某一天是星期幾 */ private static int dayOfWeek = 0; /** * 判斷是否為閏年 * * @param year * 指定的年份 * @return */ public static boolean isLeapYear(int year) { if (year % 100 == 0 && year % 400 == 0) { return true; } else if (year % 100 != 0 && year % 4 == 0) { return true; } return false; } /** * 得到某月有多少天數 * * @param isLeapyear * 目標年份 * @param month * 目標月份 * @return */ public static int getDaysOfMonth(boolean isLeapyear, int month) { switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: daysOfMonth = 31; break; case 4: case 6: case 9: case 11: daysOfMonth = 30; break; case 2: if (isLeapyear) { daysOfMonth = 29; } else { daysOfMonth = 28; } } return daysOfMonth; } /** * 指定某年中的某月的第一天是星期幾 * * @param isLeapyear * 目標年份 * @param month * 目標月份 * @return */ public static int getWeekdayOfMonth(int year, int month) { Calendar cal = Calendar.getInstance(); cal.set(year, month - 1, 1); dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1; return dayOfWeek; } /** * 獲取當前年份與月份 * * @return 返回日期數組,整形array[0],為年份,array[1]為月份, array[2]為日期 */ public static int[] getCurrentYearAndMonth() { int[] result = new int[3]; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d"); String str = ""; Date date = new Date(); str = sdf.format(date); // 當期日期 result[0] = Integer.parseInt(str.split("-")[0]); result[1] = Integer.parseInt(str.split("-")[1]); result[2] = Integer.parseInt(str.split("-")[2]); return result; }}
//日歷適配器: /**
日歷適配器 */ public class CalendarAdapter extends BaseAdapter{
private Context context; private TextView dateText; /** * 時間 */ private String[] dateTime; /** * 判斷是不是當月的日期 */ private int[] dayFlag; /** *判斷是否是已簽到日 */ private int[] signedFlag; public CalendarAdapter(Context context, String[] dateTime, int[] dayFlag, int[] signedFlag){ this.context = context; this.dateTime = dateTime; this.dayFlag = dayFlag; this.signedFlag = signedFlag; } @Override public int getCount() { // TODO Auto-generated method stub return dateTime.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { dateText = new TextView(context); dateText.setGravity(Gravity.CENTER); dateText.setTextSize(18); dateText.setWidth(46); dateText.setHeight(56); dateText.setPadding(5, 5, 5, 5); dateText.setText(dateTime[position]); //如果不是當月的日期 if(position > 6 && dayFlag[position] == 0){ dateText.setTextColor(Color.parseColor("#303030")); }else if(position > 6 && dayFlag[position] == 1){ dateText.setTextColor(Color.parseColor("#7F7F7F")); }else if(position < 7){ dateText.setTextColor(Color.parseColor("#7F7F7F")); } //判斷是否為周末,改變字體顏色 if(position > 6 && position % 7 == 0){ dateText.setTextColor(Color.parseColor("#FF5F20")); }else if(position > 6 && (position+1) % 7 ==0){ dateText.setTextColor(Color.parseColor("#FF5F20")); } //判斷是否為歷史簽到日,是則改變背景 if(dayFlag[position] == 1){ for(int index=0; index<signedFlag.length; index++){ if(dateTime[position].equals("" + signedFlag[index])){ dateText.setBackgroundColor(Color.parseColor("#FEDED2")); //如果是最新簽到的日期,改變字體,背景 if(index == 0){ dateText.setBackgroundColor(Color.parseColor("#FF5F20")); dateText.setTextColor(Color.parseColor("#FFFFFE")); } } }
}
return dateText;
}
}
//最后是使用時的方法 /**
* 當月的天數
*/
private String[] monthData;
/**
* 歷史簽到數
*/
private String[] historySignRecord;
/**
* 判斷是否為當月的
*/
private int[] dayFlag;
/**
* 是否為已簽到日
*/
private int[] daySignedFlag;
/**
* 當前年月日日期
*/
private int[] currentDate;
/**
* 一個月中的第一天是星期幾
*/
private int firstDayOfMonth;
/**
* 一個月的總天數
*/
private int daysOFMonth;
/**
* 上個月的總天數
*/
private int lastDaysOFMonth;
private int tempYear;
private int tempMonth;
private int index;
/**
* 初始化日歷相關數據
*
* @param year
* 指定年份
* @param month
* 指定月份
*/
private void initCalData(int year, int month) {
// 當月日歷顯示在49格中
int num;
monthData = new String[49];
dayFlag = new int[49];
firstDayOfMonth = DateUtils.getWeekdayOfMonth(year, month);
currentDateText.setText(year + "年" + month + "月");
// 判斷是否為閏年
if (DateUtils.isLeapYear(year)) {
daysOFMonth = DateUtils.getDaysOfMonth(true, month);
lastDaysOFMonth = DateUtils.getDaysOfMonth(true, month - 1);
} else {
daysOFMonth = DateUtils.getDaysOfMonth(false, month);
lastDaysOFMonth = DateUtils.getDaysOfMonth(false, month - 1);
}
// 前7個格子為周一到周日文字說明
for (index = 0; index < 7; index++) {
monthData[index] = ShopBaseMessage.WEEK_DATA[index];
dayFlag[index] = 0;
}
// 顯示上個月的日期
if (firstDayOfMonth == 0) {
firstDayOfMonth = 7;
}
// 如果是周日為第一天,則留一行上月的天數
for (index = 7 + firstDayOfMonth - 1; index >= 7; index--, lastDaysOFMonth--) {
monthData[index] = lastDaysOFMonth + "";
dayFlag[index] = 0;
}
// 當月的天數
for (num = 1, index = 7 + firstDayOfMonth; index < 7 + firstDayOfMonth
+ daysOFMonth; index++, num++) {
monthData[index] = num + "";
dayFlag[index] = 1;
}
// 顯示下個月的日期
for (num = 1, index = 7 + firstDayOfMonth + daysOFMonth; index < 49; index++, num++) {
monthData[index] = num + "";
dayFlag[index] = 0;
}
initSignedFlag(year, month);
calAdapter = new CalendarAdapter(this, monthData, dayFlag, daySignedFlag);
calGridView.setAdapter(calAdapter);
calGridView.setSelector(new ColorDrawable(Color.TRANSPARENT)); // 去除gridView邊框
}</pre><br />
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!