仿快捷酒店滑動選擇calendar控制元件(使用HitRect)
阿新 • • 發佈:2019-02-09
宣告:大神勿噴!!
今天我給大家講個故事:
long long ago...山裡有個公司,公司裡有個產品經理在給程式設計師講需求,需求是這樣講的:我要一個仿製快捷酒店的Calendar控制元件!!!(UI設計師在旁邊陰暗的冷笑)...
好了言歸正傳,上一篇的的HitTest讓我懂了一些基本的東西,在這裡總結下:
HitTest:
在父View抓取當前touch的X,Y座標(相對於父View)。然後與子View挨個比對,看看父View的觸點是否在子View相對於父View的範圍內。
這樣我們就能輕鬆的解決一些下面的Calenadr;
上程式碼:
activity:
acitvity的XML檔案:package com.example.clander; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; import android.util.Log; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; /** * 日曆顯示activity * * */ public class CalendarActivity extends Activity implements View.OnClickListener {//OnGestureListener, // private GestureDetector gestureDetector = null; private CalendarAdapter calV = null; private GridView gridView = null; private TextView topText = null; private static int jumpMonth = 0; //每次滑動,增加或減去一個月,預設為0(即顯示當前月) private static int jumpYear = 0; //滑動跨越一年,則增加或者減去一年,預設為0(即當前年) private int year_c = 0; private int month_c = 0; private int day_c = 0; private String currentDate = ""; private Bundle bd=null;//傳送引數 private Bundle bun=null;//接收引數 private String ruzhuTime; private String lidianTime; private String state=""; private LinearLayout upPage,nextPage; private List<View> selectedViews=new ArrayList<View>(); private boolean isPress=false; private CalendarItemEntity inEntity,outEntity; public CalendarActivity() { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d"); currentDate = sdf.format(date); //當期日期 year_c = Integer.parseInt(currentDate.split("-")[0]); month_c = Integer.parseInt(currentDate.split("-")[1]); day_c = Integer.parseInt(currentDate.split("-")[2]); } private int lastTouchIndex=-1; private int currentPosition=-1;//當前手指懸停點 private int downPositin=-1;//按下時候的點 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.calendar); bd=new Bundle();//out bun=getIntent().getExtras();//in if(bun!=null&&bun.getString("state").equals("ruzhu")) { state=bun.getString("state"); }else if(bun!=null&&bun.getString("state").equals("lidian")){ state=bun.getString("state"); } calV = new CalendarAdapter(this,getResources(),jumpMonth,jumpYear,year_c,month_c,day_c); addGridView(); gridView.setAdapter(calV); topText = (TextView) findViewById(R.id.tv_month); addTextToTopTextView(topText); gridView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent event) { int touchIndex = gridView.pointToPosition((int) event.getX(), (int) event.getY()); View childView = gridView.getChildAt(touchIndex); switch(event.getAction()){ case MotionEvent.ACTION_DOWN: lastTouchIndex=touchIndex;//初始化lastTouch isPress=true; if(selectedViews.size()!=0) selectedViews.clear(); clearAllBg(); if (null != childView){ CalendarItemEntity entity=(CalendarItemEntity) childView.getTag(); if(entity.isCheck()) downPositin=touchIndex; } case MotionEvent.ACTION_MOVE: if (null != childView){ // Log.i("texts","curr---"+currentPosition+"-----"+downPositin); currentPosition=touchIndex; CalendarItemEntity entity=(CalendarItemEntity)childView.getTag(); if(entity.isCheck()){ if(downPositin==-1) downPositin=touchIndex; currentPosition=touchIndex; Log.i("texts","curr---"+currentPosition+"-----"+downPositin); measureSelected(); lastTouchIndex=touchIndex; } } break; case MotionEvent.ACTION_UP: if(selectedViews.size()==1){ if(currentPosition>=gridView.getChildCount()-1){ addGridView(); //新增一個gridView jumpMonth++; //下一個月 calV = new CalendarAdapter(CalendarActivity.this,getResources(),jumpMonth,jumpYear,year_c,month_c,day_c); gridView.setAdapter(calV); addTextToTopTextView(topText); }else if(downPositin!=-1){ setSelectedView(gridView.getChildAt(downPositin+1)); inEntity=(CalendarItemEntity) gridView.getChildAt(downPositin).getTag(); outEntity=(CalendarItemEntity) gridView.getChildAt(downPositin+1).getTag(); } } isPress=false; downPositin=-1; Log.i("texts", ""+selectedViews.size()); Toast.makeText(CalendarActivity.this,"當前選中:"+inEntity.getYear()+inEntity.getMonth()+inEntity.getCurrentDay()+"::"+ outEntity.getYear()+outEntity.getMonth()+outEntity.getCurrentDay(), Toast.LENGTH_SHORT).show(); break; default: break; } return true; } }); upPage=(LinearLayout) this.findViewById(R.id.btn_prev_month); nextPage=(LinearLayout) this.findViewById(R.id.btn_next_month); upPage.setOnClickListener(this); nextPage.setOnClickListener(this); } private void cheekViewToAdd(View childView){ boolean isHas=false; for (View v : selectedViews) { if(v==childView) isHas=true; } if (!isHas) { selectedViews.add(childView); } } private void setSelectedView(View childView){//讓條目選中 ((TextView)((LinearLayout)childView).getChildAt(0)).setBackgroundResource(R.drawable.current_day_bgc); ((TextView)((LinearLayout)childView).getChildAt(0)).setTextColor(Color.WHITE); cheekViewToAdd(childView); } private void clearAllBg(){//清理所有背景 int childcount=gridView.getChildCount(); for (int i=0;i<childcount;i++) { LinearLayout child=(LinearLayout)gridView.getChildAt(i); CalendarItemEntity entity=(CalendarItemEntity) child.getTag(); if(!entity.isCheck()){ TextView textView=(TextView) child.getChildAt(0); textView.setBackgroundResource(R.drawable.triangle05); textView.setTextColor(Color.GRAY); continue; } TextView textView=(TextView) child.getChildAt(0); textView.setBackgroundResource(R.drawable.triangle05); textView.setTextColor(Color.BLACK); } } private void measureSelected(){ clearAllBg(); selectedViews.clear(); if(currentPosition>downPositin){//如果向右滑動 for (int i = 0; i <=currentPosition-downPositin; i++) { if(selectedViews.size()<6){ setSelectedView(gridView.getChildAt(downPositin+i)); outEntity=(CalendarItemEntity) gridView.getChildAt(downPositin+i).getTag(); } else if(Math.abs(currentPosition-downPositin)<6){ setSelectedView(gridView.getChildAt(downPositin+i)); outEntity=(CalendarItemEntity) gridView.getChildAt(downPositin+i).getTag(); } else if(currentPosition-downPositin>=6){ if(i<6){ setSelectedView(gridView.getChildAt(downPositin+i)); outEntity=(CalendarItemEntity) gridView.getChildAt(downPositin+i).getTag(); } } } }else if(currentPosition<downPositin){//向左滑動 for(int i=0;i<=downPositin-currentPosition;i++){ if(selectedViews.size()<6) setSelectedView(gridView.getChildAt(downPositin-i)); else if(Math.abs(downPositin-currentPosition)<6) setSelectedView(gridView.getChildAt(downPositin-i)); else if(downPositin-currentPosition>=6){ if(i<6) setSelectedView(gridView.getChildAt(downPositin-i)); } Log.i("texts", "ttttt::::"+currentPosition+"---"+downPositin); } }else{ setSelectedView(gridView.getChildAt(currentPosition)); } } //新增頭部的年份 閏哪月等資訊 public void addTextToTopTextView(TextView view){ StringBuffer textDate = new StringBuffer(); textDate.append(calV.getShowYear()).append("年").append( calV.getShowMonth()).append("月").append("\t"); view.setText(textDate); view.setTextColor(Color.WHITE); view.setTypeface(Typeface.DEFAULT_BOLD); } //新增gridview private void addGridView() { gridView =(GridView)findViewById(R.id.gridview); // gridView.setOnTouchListener(new OnTouchListener() { // //將gridview中的觸控事件回傳給gestureDetector // @Override // public boolean onTouch(View v, MotionEvent event) { // // TODO Auto-generated method stub // return CalendarActivity.this.gestureDetector.onTouchEvent(event); // } // }); gridView.setOnItemClickListener(new OnItemClickListener() { //gridView中的每一個item的點選事件 @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { //點選任何一個item,得到這個item的日期(排除點選的是週日到週六(點選不響應)) int startPosition = calV.getStartPositon(); int endPosition = calV.getEndPosition(); if(startPosition <= position+7 && position <= endPosition-7){ String scheduleDay = calV.getDateByClickItem(position).split("\\.")[0]; //這一天的陽曆 //String scheduleLunarDay = calV.getDateByClickItem(position).split("\\.")[1]; //這一天的陰曆 String scheduleYear = calV.getShowYear(); String scheduleMonth = calV.getShowMonth(); // Toast.makeText(CalendarActivity.this, scheduleYear+"-"+scheduleMonth+"-"+scheduleDay, 2000).show(); ruzhuTime=scheduleMonth+"月"+scheduleDay+"日"; lidianTime=scheduleMonth+"月"+scheduleDay+"日"; Intent intent=new Intent(); if(state.equals("ruzhu")) { bd.putString("ruzhu", ruzhuTime); System.out.println("ruzhuuuuuu"+bd.getString("ruzhu")); }else if(state.equals("lidian")){ bd.putString("lidian", lidianTime); } } } }); } @Override public void onClick(View v) { switch(v.getId()){ case R.id.btn_prev_month://上一頁 addGridView(); //新增一個gridView jumpMonth--; //上一個月 calV = new CalendarAdapter(this,getResources(),jumpMonth,jumpYear,year_c,month_c,day_c); gridView.setAdapter(calV); downPositin=-1; addTextToTopTextView(topText); break; case R.id.btn_next_month://下一頁 addGridView(); //新增一個gridView jumpMonth++; //下一個月 calV = new CalendarAdapter(this,getResources(),jumpMonth,jumpYear,year_c,month_c,day_c); gridView.setAdapter(calV); addTextToTopTextView(topText); downPositin=-1; break; default: break; } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="40.0dip" android:background="#ff394147" android:gravity="center_vertical" > <LinearLayout android:id="@+id/btn_prev_month" android:layout_width="0.0dip" android:layout_height="fill_parent" android:layout_weight="0.2" android:gravity="center" android:clickable="true" android:orientation="vertical" > <TextView android:id="@+id/left_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/triangle06_states" android:duplicateParentState="true" android:text="" android:textSize="16.0dip" /> </LinearLayout> <LinearLayout android:layout_width="0.0dip" android:layout_height="fill_parent" android:layout_weight="0.6" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/tv_month" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="xxxx年14月" android:textColor="#ffffffff" android:textSize="21.0dip" /> </LinearLayout> <LinearLayout android:id="@+id/btn_next_month" android:layout_width="0.0dip" android:layout_height="fill_parent" android:layout_weight="0.2" android:gravity="center" android:clickable="true" android:orientation="vertical" > <TextView android:id="@+id/right_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/triangle05_states" android:duplicateParentState="true" android:text="" android:textSize="16.0dip" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="22.0dip" android:background="#ff8091a8" > <TextView style="@style/weekName" android:text="週日" /> <TextView style="@style/weekName" android:text="週一" /> <TextView style="@style/weekName" android:text="週二" /> <TextView style="@style/weekName" android:text="週三" /> <TextView style="@style/weekName" android:text="週四" /> <TextView style="@style/weekName" android:text="週五" /> <TextView style="@style/weekName" android:text="週六" /> </LinearLayout> <GridView android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#00000000" android:clickable="true" android:clipChildren="true" android:columnWidth="55.0dip" android:horizontalSpacing="1.0dip" android:listSelector="@null" android:numColumns="7" android:paddingBottom="1.0dip" android:stretchMode="columnWidth" android:verticalSpacing="1.0dip" /> </LinearLayout>
item的layout檔案:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="#FFFFFF" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:gravity="center" android:layout_width="fill_parent" android:layout_height="55dp" android:id="@+id/tvtext" /> </LinearLayout>
adapter介面卡java檔案:
package com.example.clander;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
/**
* 日曆gridview中的每一個item顯示的textview
*
* @author lmw
*
*/
public class CalendarAdapter extends BaseAdapter {
public boolean isLeapyear = false; // 是否為閏年
public int daysOfMonth = 0; // 某月的天數
public int dayOfWeek = 0; // 具體某一天是星期幾
public int lastDaysOfMonth = 0; // 上一個月的總天數
public Context context;
public String[] dayNumber = new String[42]; // 一個gridview中的日期存入此陣列中
// private static String week[] = {"週日","週一","週二","週三","週四","週五","週六"};
public SpecialCalendar sc = null;
// private LunarCalendar lc = null;
public Resources res = null;
public Drawable drawable = null;
public String currentYear = "";
public String currentMonth = "";
public String currentDay = "";
public SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
public int currentFlag = -1; // 用於標記當天
public int[] schDateTagFlag = null; // 儲存當月所有的日程日期
public String showYear = ""; // 用於在頭部顯示的年份
public String showMonth = ""; // 用於在頭部顯示的月份
public String animalsYear = "";
public String leapMonth = ""; // 閏哪一個月
// public String cyclical = ""; //天干地支
// 系統當前時間
public String sysDate = "";
public String sys_year = "";
public String sys_month = "";
public String sys_day = "";
private Calendar currCalendar;
private int currDay;
private int currMonth;
private int currYear;
private int selectedSize=90;//可選天數
private int wantYear;
private int wantMonth;
private int wantDay;
private Calendar wantCalendar;
public CalendarAdapter() {
Date date = new Date();
sysDate = sdf.format(date); // 當期日期
sys_year = sysDate.split("-")[0];
sys_month = sysDate.split("-")[1];
sys_day = sysDate.split("-")[2];
currCalendar = Calendar.getInstance();
currDay = currCalendar.get(Calendar.DAY_OF_MONTH);
currMonth = currCalendar.get(Calendar.MONTH) + 1;
currYear = currCalendar.get(Calendar.YEAR);
wantCalendar=(Calendar) currCalendar.clone();
wantCalendar.add(Calendar.DAY_OF_YEAR,selectedSize);
wantYear=wantCalendar.get(Calendar.YEAR);
wantMonth=wantCalendar.get(Calendar.MONTH)+1;
wantDay=wantCalendar.get(Calendar.DAY_OF_MONTH);
}
public CalendarAdapter(Context context, Resources rs, int jumpMonth,
int jumpYear, int year_c, int month_c, int day_c) {
this();
this.context = context;
sc = new SpecialCalendar();
// lc = new LunarCalendar();
this.res = rs;
int stepYear = year_c + jumpYear;
int stepMonth = month_c + jumpMonth;
if (stepMonth > 0) {
// 往下一個月滑動
if (stepMonth % 12 == 0) {
stepYear = year_c + stepMonth / 12 - 1;
stepMonth = 12;
} else {
stepYear = year_c + stepMonth / 12;
stepMonth = stepMonth % 12;
}
} else {
// 往上一個月滑動
stepYear = year_c - 1 + stepMonth / 12;
stepMonth = stepMonth % 12 + 12;
if (stepMonth % 12 == 0) {
}
}
currentYear = String.valueOf(stepYear);// 得到當前的年份
currentMonth = String.valueOf(stepMonth); // 得到本月
// (jumpMonth為滑動的次數,每滑動一次就增加一月或減一月)
currentDay = String.valueOf(day_c); // 得到當前日期是哪天
getCalendar(Integer.parseInt(currentYear),
Integer.parseInt(currentMonth));
}
public CalendarAdapter(Context context, Resources rs, int year, int month,
int day) {
this();
this.context = context;
sc = new SpecialCalendar();
// lc = new LunarCalendar();
this.res = rs;
currentYear = String.valueOf(year); // 得到跳轉到的年份
currentMonth = String.valueOf(month); // 得到跳轉到的月份
currentDay = String.valueOf(day); // 得到跳轉到的天
getCalendar(Integer.parseInt(currentYear),
Integer.parseInt(currentMonth));
}
@Override
public int getCount() {
return dayNumber.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CalendarItemEntity entity = new CalendarItemEntity();// 條目實體
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.calendar_item, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.tvtext);
String d = dayNumber[position].split("\\.")[0];
String dv = dayNumber[position].split("\\.")[1];
// SpannableString sp = new SpannableString(d+"\n"+dv);
SpannableString sp = new SpannableString(d);
sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0,
d.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
sp.setSpan(new RelativeSizeSpan(1.2f), 0, d.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(sp);
textView.setTextColor(Color.GRAY);
entity.setCheck(false);
entity.setCurrentDay(Integer.parseInt("" + sp));
Calendar ca=(Calendar) currCalendar.clone();
ca.add(Calendar.DAY_OF_MONTH,-1);
long ldv=Long.parseLong(dv);
if(ca.getTimeInMillis()<=ldv&&ldv<=wantCalendar.getTimeInMillis()){
textView.setTextColor(Color.BLACK);// 當月字型設黑
drawable = res.getDrawable(R.drawable.current_day_bgc);
entity.setCheck(true);
}
if(position < dayOfWeek){
if(cMonth==1){
entity.setMonth(12);
entity.setYear(cYear-1);
}else{
entity.setMonth(cMonth-1);
entity.setYear(cYear);
}
}else if (position < daysOfMonth + dayOfWeek&& position >= dayOfWeek) {
entity.setMonth(cMonth);
entity.setYear(cYear);
}else if(position>=daysOfMonth+dayOfWeek){
if(cMonth==12){
entity.setMonth(1);
entity.setYear(cYear+1);
}else{
entity.setMonth(cMonth+1);
entity.setYear(cYear);
}
}
if (schDateTagFlag != null && schDateTagFlag.length > 0) {
for (int i = 0; i < schDateTagFlag.length; i++) {
if (schDateTagFlag[i] == position) {
// 設定日程標記背景
textView.setBackgroundResource(R.drawable.mark);
}
}
}
if (currentFlag == position) {
// 設定當天的背景
drawable = res.getDrawable(R.drawable.current_day_bgc);
textView.setBackgroundDrawable(drawable);
textView.setText("今天");
textView.setTextColor(Color.WHITE);
}
if(currentFlag>0&¤tFlag+1==position){
drawable = res.getDrawable(R.drawable.current_day_bgc);
textView.setBackgroundDrawable(drawable);
textView.setText("明天");
textView.setTextColor(Color.WHITE);
}
convertView.setTag(entity);
return convertView;
}
private int cMonth;
private int cYear;
// 得到某年的某月的天數且這月的第一天是星期幾
public void getCalendar(int year, int month) {
isLeapyear = sc.isLeapYear(year); // 是否為閏年
daysOfMonth = sc.getDaysOfMonth(isLeapyear, month); // 某月的總天數
dayOfWeek = sc.getWeekdayOfMonth(year, month); // 某月第一天為星期幾
lastDaysOfMonth = sc.getDaysOfMonth(isLeapyear, month - 1); // 上一個月的總天數
Log.d("DAY", isLeapyear + " ====== " + daysOfMonth
+ " ============ " + dayOfWeek + " ========= "
+ lastDaysOfMonth);
cMonth = month;
cYear = year;
Calendar c = Calendar.getInstance();
c.setTime(new Date(currYear, currMonth, currDay));
c.set(currYear, currMonth - 1, currDay);
System.out.println(currYear + "年 " + currMonth + "月 " + currDay
+ "日為星期:" + (c.get(Calendar.DAY_OF_WEEK) - 1));
cWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
getweek(year, month);
}
private int cWeek;
// 將一個月中的每一天的值新增入陣列dayNuber中
private void getweek(int year, int month) {
int j = 1;
int flag = 0;
String lunarDay = "";
for (int i = 0; i < dayNumber.length; i++) {
Calendar c = Calendar.getInstance();
if (i < dayOfWeek) { // 前一個月
int temp = lastDaysOfMonth - dayOfWeek + 1;
// lunarDay = lc.getLunarDate(year, month-1, temp+i,false);
c.set(year, month-2,temp+i);
dayNumber[i] = (temp + i) + "." + c.getTimeInMillis();
} else if (i < daysOfMonth + dayOfWeek) { // 本月
String day = String.valueOf(i - dayOfWeek + 1); // 得到的日期
// lunarDay = lc.getLunarDate(year, month,
// i-dayOfWeek+1,false);
c.set(year, month-1,Integer.parseInt(day));
dayNumber[i] = i - dayOfWeek + 1 + "." + c.getTimeInMillis();
// 對於當前月才去標記當前日期
if (sys_year.equals(String.valueOf(year))
&& sys_month.equals(String.valueOf(month))
&& sys_day.equals(day)) {
// 標記當前日期
currentFlag = i;
}
setShowYear(String.valueOf(year));
setShowMonth(String.valueOf(month));
} else { // 下一個月
c.set(year, month,j);
dayNumber[i] = j + "." + c.getTimeInMillis();
j++;
}
}
// }
currCalendar = Calendar.getInstance();// 重置當前時間的calendar
String abc = "";
for (int i = 0; i < dayNumber.length; i++) {
abc = abc + dayNumber[i] + ":";
}
Log.d("DAYNUMBER", abc);
}
public void matchScheduleDate(int year, int month, int day) {
}
/**
* 點選每一個item時返回item中的日期
*
* @param position
* @return
*/
public String getDateByClickItem(int position) {
return dayNumber[position];
}
/**
* 在點選gridView時,得到這個月中第一天的位置
*
* @return
*/
public int getStartPositon() {
return dayOfWeek + 7;
}
/**
* 在點選gridView時,得到這個月中最後一天的位置
*
* @return
*/
public int getEndPosition() {
return (dayOfWeek + daysOfMonth + 7) - 1;
}
public String getShowYear() {
return showYear;
}
public void setShowYear(String showYear) {
this.showYear = showYear;
}
public String getShowMonth() {
return showMonth;
}
public void setShowMonth(String showMonth) {
this.showMonth = showMonth;
}
public String getAnimalsYear() {
return animalsYear;
}
public void setAnimalsYear(String animalsYear) {
this.animalsYear = animalsYear;
}
public String getLeapMonth() {
return leapMonth;
}
public void setLeapMonth(String leapMonth) {
this.leapMonth = leapMonth;
}
}
javaBean(實體類):
package com.example.clander;
import java.io.Serializable;
public class CalendarItemEntity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 7086289624407287042L;
private boolean isCheck=false;
private int currentDay=-1;
private int year;
private int month;
public boolean isCheck() {
return isCheck;
}
public void setCheck(boolean isCheck) {
this.isCheck = isCheck;
}
public int getCurrentDay() {
return currentDay;
}
public void setCurrentDay(int currentDay) {
this.currentDay = currentDay;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
}
Calendar核心處理類:
package com.example.clander;
import java.util.Calendar;
public class SpecialCalendar {
private int daysOfMonth = 0; //某月的天數
private int dayOfWeek = 0; //具體某一天是星期幾
// 判斷是否為閏年
public boolean isLeapYear(int year) {
if (year % 100 == 0 && year % 400 == 0) {
return true;
} else if (year % 100 != 0 && year % 4 == 0) {
return true;
}
return false;
}
//得到某月有多少天數
public int getDaysOfMonth(boolean isLeapyear, int month) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
daysOfMonth = 31;
break;
case 4:
case 6:
case 9:
case 11:
daysOfMonth = 30;
break;
case 2:
if (isLeapyear) {
daysOfMonth = 29;
} else {
daysOfMonth = 28;
}
}
return daysOfMonth;
}
//指定某年中的某月的第一天是星期幾
public int getWeekdayOfMonth(int year, int month){
Calendar cal = Calendar.getInstance();
cal.set(year, month-1, 1);
dayOfWeek = cal.get(Calendar.DAY_OF_WEEK)-1;
return dayOfWeek;
}
}
OK。。。看似複雜,真心很容易通俗易懂。只要耐心看下去,沒有什麼難的
這是效果圖:
寫完之後嘆了口氣,洗了把臉,然後高喊一聲:"測試小妹!!!咱們放學後小巷子裡,6元麻辣燙,我請!!!!"