这里主要记录一下在编写日历apk过程中一些主要的点:先看下效果图

一、主要功能
- 1、支持农历、节气、常用节假日
- 2、使用数据库,设置计划
二、基本结构
我们要实现的日历控件采用GestureDetector构造器,使用OnGestureListener监听滑动。目前我们设定日历左右滑动为月份切换的操作,每一个月份显示通过GridView实现,里面的数据是通过构造CalendarView,将其绑定到GridView。每次左右滑动,都会刷新CalendarView。布局上,会在GridView上显示当前日期农历时间。
三、主要代码及解析
CalendarActivity.java
/**
* 日历显示activity
*
* @author 孔乙己大叔
*/
public class CalendarActivity extends Activity implements OnGestureListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gestureDetector = new GestureDetector(this, this);
flipper = (ViewFlipper) findViewById(R.id.flipper);
flipper.removeAllViews();
calV = new CalendarView(this, getResources(), jumpMonth, jumpYear, year_c, month_c, day_c);
addGridView();
gridView.setAdapter(calV);
flipper.addView(gridView, 0);
topText = (BorderText) findViewById(R.id.toptext);
addTextToTopTextView(topText);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
int gvFlag = 0; //每次添加gridview到viewflipper中时给的标记
if (e1.getX() - e2.getX() > 120) {
//像左滑动
addGridView(); //添加一个gridView
jumpMonth++; //下一个月
calV = new CalendarView(this, getResources(), jumpMonth, jumpYear, year_c, month_c, day_c);
gridView.setAdapter(calV);
addTextToTopTextView(topText);
gvFlag++;
flipper.addView(gridView, gvFlag);
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
this.flipper.showNext();
flipper.removeViewAt(0);
return true;
} else if (e1.getX() - e2.getX() < -120) {
//向右滑动
addGridView(); //添加一个gridView
jumpMonth--; //上一个月
calV = new CalendarView(this, getResources(), jumpMonth, jumpYear, year_c, month_c, day_c);
gridView.setAdapter(calV);
gvFlag++;
addTextToTopTextView(topText);
//flipper.addView(gridView);
flipper.addView(gridView, gvFlag);
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
this.flipper.showPrevious();
flipper.removeViewAt(0);
return true;
}
return false;
}
/**
* 创建菜单
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, menu.FIRST, menu.FIRST, "今天");
menu.add(0, menu.FIRST + 1, menu.FIRST + 1, "跳转");
menu.add(0, menu.FIRST + 2, menu.FIRST + 2, "日程");
menu.add(0, menu.FIRST + 3, menu.FIRST + 3, "日期转换");
return super.onCreateOptionsMenu(menu);
}
/**
* 选择菜单
*/
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case Menu.FIRST:
//跳转到今天
int xMonth = jumpMonth;
int xYear = jumpYear;
int gvFlag = 0;
jumpMonth = 0;
jumpYear = 0;
addGridView(); //添加一个gridView
year_c = Integer.parseInt(currentDate.split("-")[0]);
month_c = Integer.parseInt(currentDate.split("-")[1]);
day_c = Integer.parseInt(currentDate.split("-")[2]);
calV = new CalendarView(this, getResources(), jumpMonth, jumpYear, year_c, month_c, day_c);
gridView.setAdapter(calV);
addTextToTopTextView(topText);
gvFlag++;
flipper.addView(gridView, gvFlag);
if (xMonth == 0 && xYear == 0) {
//nothing to do
} else if ((xYear == 0 && xMonth > 0) || xYear > 0) {
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out)

本文详细介绍了日历App的开发过程,包括支持农历、节气、节假日等功能,使用GestureDetector和GridView实现月份切换,以及自定义CalendarView显示农历信息。同时,文章提供了关键代码示例,如滑动切换月份、跳转到特定日期、日程管理和日期转换功能。
最低0.47元/天 解锁文章
27

被折叠的 条评论
为什么被折叠?



