Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 周歷

Android 周歷

編輯:關於Android編程

main.xml 文件


xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >


android:id="@+id/ll_week"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >


android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="周日" />


android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="周一" />


android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="周二" />


android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="周三" />


android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="周四" />


android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="周五" />


android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="周六" />



android:id="@+id/flipper1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/ll_week" />
android:id="@+id/tv_date"
android:layout_below="@id/flipper1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2014年5月6日"
android:gravity="center"
/>





item_calendar.xml文件

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >


android:id="@+id/tv_calendar"
android:layout_width="30dp"
android:layout_height="30dp"
android:gravity="center" />







MainActivity.java

public class MainActivity extends Activity implements OnGestureListener {

private ViewFlipper flipper1 = null;
private GridView gridView = null;
private GestureDetector gestureDetector = null;
private int year_c = 0;
private int month_c = 0;
private int day_c = 0;
private int week_c = 0;
private int week_num = 0;
private String currentDate = "";
private static int jumpWeek = 0;
private static int jumpMonth = 0;
private static int jumpYear = 0;
private DateAdapter dateAdapter;
private int daysOfMonth = 0; // 某月的天數
private int dayOfWeek = 0; // 具體某月的第一天是星期幾
private int weeksOfMonth = 0;
private SpecialCalendar sc = null;
private boolean isLeapyear = false; // 是否為閏年
private int selectPostion = 0;
private String dayNumbers[] = new String[7];
private TextView tvDate;
private int currentYear;
private int currentMonth;
private int currentWeek;
private int currentDay;
private int currentNum;
private boolean isStart;// 是否是交接的月初


public MainActivity() {


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]);//9


currentYear = year_c;
currentMonth = month_c;
currentDay = day_c;

sc = new SpecialCalendar();

getCalendar(year_c, month_c);

week_num = getWeeksOfMonth();//12月份week_num是5


currentNum = week_num;//5


if (dayOfWeek == 7) {
week_c = day_c / 7 + 1;
} else {
if (day_c <= (7 - dayOfWeek)) {
week_c = 1;
} else {
if ((day_c - (7 - dayOfWeek)) % 7 == 0) {
week_c = (day_c - (7 - dayOfWeek)) / 7 + 1;
Log.i("life", "day_c = "+ day_c );
} else {
week_c = (day_c - (7 - dayOfWeek)) / 7 + 2;
}
}
}
currentWeek = week_c;

Log.i("life", " currentWeek = "+currentWeek );

getCurrent();


}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



tvDate = (TextView) findViewById(R.id.tv_date);
tvDate.setText(year_c + "年" + month_c + "月" + day_c + "日");


gestureDetector = new GestureDetector(this);
flipper1 = (ViewFlipper) findViewById(R.id.flipper1);
dateAdapter = new DateAdapter(this, getResources(), currentYear,
currentMonth, currentWeek, currentNum, selectPostion,
currentWeek == 1 ? true : false);
addGridView();
dayNumbers = dateAdapter.getDayNumbers();
gridView.setAdapter(dateAdapter);
selectPostion = dateAdapter.getTodayPosition();
gridView.setSelection(selectPostion);
flipper1.addView(gridView, 0);
}



/**
* 判斷某年某月所有的星期數
*
* @param year
* @param month
*/
public int getWeeksOfMonth(int year, int month) {
// 先判斷某月的第一天為星期幾
int preMonthRelax = 0;
int dayFirst = getWhichDayOfWeek(year, month);
int days = sc.getDaysOfMonth(sc.isLeapYear(year), month);
if (dayFirst != 7) {
preMonthRelax = dayFirst;
}
if ((days + preMonthRelax) % 7 == 0) {
weeksOfMonth = (days + preMonthRelax) / 7;
} else {
weeksOfMonth = (days + preMonthRelax) / 7 + 1;
}
return weeksOfMonth;


}


/**
* 判斷某年某月的第一天為星期幾
*
* @param year
* @param month
* @return
*/
public int getWhichDayOfWeek(int year, int month) {
return sc.getWeekdayOfMonth(year, month);


}


/**
*
* @param year
* @param month
*/
public int getLastDayOfWeek(int year, int month) {

return sc.getWeekDayOfLastMonth(year, month,
sc.getDaysOfMonth(isLeapyear, month));
}


public void getCalendar(int year, int month) {
isLeapyear = sc.isLeapYear(year); // 是否為閏年
daysOfMonth = sc.getDaysOfMonth(isLeapyear, month); // 某月的總天數
dayOfWeek = sc.getWeekdayOfMonth(year, month); // 某月第一天為星期幾

Log.i("life", " dayOfWeek = "+dayOfWeek );
}


public int getWeeksOfMonth() {

int preMonthRelax = 0;
if (dayOfWeek != 7) {//2014年的12月1號是星期一,所以preMonthRelax的值是1
preMonthRelax = dayOfWeek;
}



if ((daysOfMonth + preMonthRelax) % 7 == 0) {
weeksOfMonth = (daysOfMonth + preMonthRelax) / 7;
} else {
weeksOfMonth = (daysOfMonth + preMonthRelax) / 7 + 1;//所以2014年的12月的weeksOfMonth值是1
}

Log.i("life", " weeksOfMonth = "+weeksOfMonth );

return weeksOfMonth;
}





private void addGridView() {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
gridView = new GridView(this);
gridView.setNumColumns(7);
gridView.setGravity(Gravity.CENTER_VERTICAL);
gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
gridView.setVerticalSpacing(1);
gridView.setHorizontalSpacing(1);
gridView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return MainActivity.this.gestureDetector.onTouchEvent(event);
}
});


gridView.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {

selectPostion = position;
dateAdapter.setSeclection(position);
dateAdapter.notifyDataSetChanged();
tvDate.setText(dateAdapter.getCurrentYear(selectPostion) + "年"
+ dateAdapter.getCurrentMonth(selectPostion) + "月"
+ dayNumbers[position] + "日");
}
});
gridView.setLayoutParams(params);
}


@Override
protected void onPause() {
super.onPause();
jumpWeek = 0;
}


@Override
public boolean onDown(MotionEvent e) {


return false;
}


@Override
public void onShowPress(MotionEvent e) {


}


@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}


@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
return false;
}


@Override
public void onLongPress(MotionEvent e) {


}


/**
* 重新計算當前的年月
*/
public void getCurrent() {
if (currentWeek > currentNum) {
if (currentMonth + 1 <= 12) {
currentMonth++;
} else {
currentMonth = 1;
currentYear++;
}
currentWeek = 1;
currentNum = getWeeksOfMonth(currentYear, currentMonth);
} else if (currentWeek == currentNum) {
if (getLastDayOfWeek(currentYear, currentMonth) == 6) {
} else {
if (currentMonth + 1 <= 12) {
currentMonth++;
} else {
currentMonth = 1;
currentYear++;
}
currentWeek = 1;
currentNum = getWeeksOfMonth(currentYear, currentMonth);
}


} else if (currentWeek < 1) {
if (currentMonth - 1 >= 1) {
currentMonth--;
} else {
currentMonth = 12;
currentYear--;
}
currentNum = getWeeksOfMonth(currentYear, currentMonth);
currentWeek = currentNum - 1;
}
}


@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
int gvFlag = 0;
if (e1.getX() - e2.getX() > 80) {
// 向左滑
addGridView();
currentWeek++;
getCurrent();
dateAdapter = new DateAdapter(this, getResources(), currentYear,
currentMonth, currentWeek, currentNum, selectPostion,
currentWeek == 1 ? true : false);
dayNumbers = dateAdapter.getDayNumbers();
gridView.setAdapter(dateAdapter);
tvDate.setText(dateAdapter.getCurrentYear(selectPostion) + "年"
+ dateAdapter.getCurrentMonth(selectPostion) + "月"
+ dayNumbers[selectPostion] + "日");
gvFlag++;
flipper1.addView(gridView, gvFlag);
dateAdapter.setSeclection(selectPostion);
this.flipper1.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_in));
this.flipper1.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
this.flipper1.showNext();
flipper1.removeViewAt(0);
return true;


} else if (e1.getX() - e2.getX() < -80) {
addGridView();
currentWeek--;
getCurrent();
dateAdapter = new DateAdapter(this, getResources(), currentYear,
currentMonth, currentWeek, currentNum, selectPostion,
currentWeek == 1 ? true : false);
dayNumbers = dateAdapter.getDayNumbers();
gridView.setAdapter(dateAdapter);
tvDate.setText(dateAdapter.getCurrentYear(selectPostion) + "年"
+ dateAdapter.getCurrentMonth(selectPostion) + "月"
+ dayNumbers[selectPostion] + "日");
gvFlag++;
flipper1.addView(gridView, gvFlag);
dateAdapter.setSeclection(selectPostion);
this.flipper1.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_in));
this.flipper1.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_out));
this.flipper1.showPrevious();
flipper1.removeViewAt(0);
return true;
// }
}
return false;
}


@Override
public boolean onTouchEvent(MotionEvent event) {
return this.gestureDetector.onTouchEvent(event);
}


}




SpecialCalendar.java

public class SpecialCalendar {


private int daysOfMonth = 0; //某月的天數
private int dayOfWeek = 0; //具體某一天是星期幾
private int eachDayOfWeek = 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;
}
public int getWeekDayOfLastMonth(int year,int month,int day){
Calendar cal = Calendar.getInstance();
cal.set(year, month-1, day);
eachDayOfWeek = cal.get(Calendar.DAY_OF_WEEK)-1;
return eachDayOfWeek;
}

}



DateAdapter.java

public class DateAdapter extends BaseAdapter {


private boolean isLeapyear = false; // 是否為閏年
private int daysOfMonth = 0; // 某月的天數
private int dayOfWeek = 0; // 具體某一天是星期幾
private int nextDayOfWeek = 0;
private int lastDayOfWeek = 0;
private int lastDaysOfMonth = 0; // 上一個月的總天數
private int eachDayOfWeek = 0;
private Context context;
private SpecialCalendar sc = null;
private Resources res = null;
private Drawable drawable = null;
private String[] dayNumber = new String[7];
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
private int currentFlag = -1; // 用於標記當天
// 系統當前時間
private String sysDate = "";
private String sys_year = "";
private String sys_month = "";
private String sys_day = "";
private String currentYear = "";
private String currentMonth = "";
private String currentWeek = "";
private String currentDay = "";
private int weeksOfMonth;
private int default_postion;
private int clickTemp = -1;
private int week_num = 0;
private int week_c = 0;
private int month = 0;
private int jumpWeek = 0;
private int c_month = 0;
private int c_day_week = 0;
private int n_day_week = 0;
private boolean isStart;


// 標識選擇的Item
public void setSeclection(int position) {
clickTemp = position;
}


public DateAdapter() {
Date date = new Date();
sysDate = sdf.format(date); // 當期日期
sys_year = sysDate.split("-")[0];
sys_month = sysDate.split("-")[1];
sys_day = sysDate.split("-")[2];
month = Integer.parseInt(sys_month);
}


public DateAdapter(Context context, Resources rs, int year_c, int month_c,
int week_c, int week_num, int default_postion, boolean isStart) {
this();
this.context = context;
this.res = rs;
this.default_postion = default_postion;
this.week_c = week_c;
this.isStart = isStart;
sc = new SpecialCalendar();


lastDayOfWeek = sc.getWeekDayOfLastMonth(year_c, month_c,
sc.getDaysOfMonth(sc.isLeapYear(year_c), month_c));

Log.i("life", " lastDayOfWeek = "+lastDayOfWeek );

currentYear = String.valueOf(year_c);
// 得到當前的年份
currentMonth = String.valueOf(month_c); // 得到本月
// (jumpMonth為滑動的次數,每滑動一次就增加一月或減一月)
currentDay = String.valueOf(sys_day);

// 得到當前日期是哪天
getCalendar(Integer.parseInt(currentYear),
Integer.parseInt(currentMonth));

currentWeek = String.valueOf(week_c);

Log.i("life", " week_c = "+week_c );

getWeek(Integer.parseInt(currentYear), Integer.parseInt(currentMonth),
Integer.parseInt(currentWeek));


}


public int getTodayPosition() {
int todayWeek = sc.getWeekDayOfLastMonth(Integer.parseInt(sys_year),
Integer.parseInt(sys_month), Integer.parseInt(sys_day));
if (todayWeek == 7) {
clickTemp = 0;
} else {
clickTemp = todayWeek;
}
return clickTemp;
}


public int getCurrentMonth(int position) {

int thisDayOfWeek = sc.getWeekdayOfMonth(Integer.parseInt(currentYear),
Integer.parseInt(currentMonth));

if (isStart) {
if (thisDayOfWeek != 7) {
if (position < thisDayOfWeek) {
return Integer.parseInt(currentMonth) - 1 == 0 ? 12
: Integer.parseInt(currentMonth) - 1;
} else {
return Integer.parseInt(currentMonth);
}
} else {
return Integer.parseInt(currentMonth);
}
} else {
return Integer.parseInt(currentMonth);
}


}


public int getCurrentYear(int position) {
int thisDayOfWeek = sc.getWeekdayOfMonth(Integer.parseInt(currentYear),
Integer.parseInt(currentMonth));
if (isStart) {
if (thisDayOfWeek != 7) {
if (position < thisDayOfWeek) {
return Integer.parseInt(currentMonth) - 1 == 0 ? Integer
.parseInt(currentYear) - 1 : Integer
.parseInt(currentYear);
} else {
return Integer.parseInt(currentYear);
}
} else {
return Integer.parseInt(currentYear);
}
} else {
return Integer.parseInt(currentYear);
}
}


public void getCalendar(int year, int month) {
isLeapyear = sc.isLeapYear(year); // 是否為閏年
daysOfMonth = sc.getDaysOfMonth(isLeapyear, month); // 某月的總天數
dayOfWeek = sc.getWeekdayOfMonth(year, month); // 某月第一天為星期幾 1
lastDaysOfMonth = sc.getDaysOfMonth(isLeapyear, month - 1);
nextDayOfWeek = sc.getDaysOfMonth(isLeapyear, month + 1);
}


public void getWeek(int year, int month, int week) {

for (int i = 0; i < dayNumber.length; i++) {
if (dayOfWeek == 7) {
dayNumber[i] = String.valueOf((i + 1) + 7 * (week - 1));
} else {
if (week == 1) {
if (i < dayOfWeek) {

Log.i("life", " lastDaysOfMonth = "+lastDaysOfMonth );

dayNumber[i] = String.valueOf(lastDaysOfMonth
- (dayOfWeek - (i + 1)));
} else {
dayNumber[i] = String.valueOf(i - dayOfWeek + 1);
}
} else {
dayNumber[i] = String.valueOf((7 - dayOfWeek + 1 + i) + 7
* (week - 2));
}
}
}
}


public String[] getDayNumbers() {
return dayNumber;
}


/**
* 得到某月有幾周(特殊算法)
*/
public int getWeeksOfMonth() {

int preMonthRelax = 0;
if (dayOfWeek != 7) {
preMonthRelax = dayOfWeek;
}
if ((daysOfMonth + preMonthRelax) % 7 == 0) {
weeksOfMonth = (daysOfMonth + preMonthRelax) / 7;
} else {
weeksOfMonth = (daysOfMonth + preMonthRelax) / 7 + 1;
}
return weeksOfMonth;
}


/**
* 某一天在第幾周
*/
public void getDayInWeek(int year, int month) {


}


@Override
public int getCount() {
// TODO Auto-generated method stub
return dayNumber.length;
}


@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.item_calendar, null);
}
TextView tvCalendar = (TextView) convertView
.findViewById(R.id.tv_calendar);
tvCalendar.setText(dayNumber[position]);
if (clickTemp == position) {
tvCalendar.setSelected(true);
tvCalendar.setTextColor(Color.WHITE);
tvCalendar.setBackgroundResource(R.drawable.circle_message);
} else {
tvCalendar.setSelected(false);
tvCalendar.setTextColor(Color.BLACK);
tvCalendar.setBackgroundColor(Color.TRANSPARENT);
}
return convertView;
}


}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved