- 注:此功能在在Activity中,由三大块组成:头(上月按扭,下月按扭,当前年月文本),日历块(星期区域,日期区域),描述区域
- Activity:
- /**
- * Android实现日历控件
- * 注:Calendar时间与现在实的时间在月份上需要+1,因为月份是0-11
- */
- public class CalenderActivity extends Activity {
- http://m.nvzi91.cn/gongjingmilan/29426.html
- private ArrayList<DateWidgetDayView> days = new ArrayList<DateWidgetDayView>();
- //颜色代码常量
- public static int Calendar_WeekBgColor = 0;
- public static int Calendar_DayBgColor = 0;
- public static int IsHoliday_BgColor = 0;
- public static int UnPresentMonth_FontColor = 0;
- public static int IsPresentMonth_FontColor = 0;
- public static int IsToday_BgColor = 0;
- public static int Special_Reminder = 0;
- public static int Common_Reminder = 0;
- public static int Calendar_WeekFontColor = 0;
- /**
- * 表格中的第一天,一般上月的某一天
- */
- public static Calendar mFirstDateOfPanel = Calendar.getInstance();
- private Calendar mTodayDate = Calendar.getInstance();// 初始日期,即当天
- private Calendar mSelectedDate = Calendar.getInstance();//选中的日期,如果未选中则为1970-1-1
- private Calendar mViewDate = Calendar.getInstance();
- // 当前操作日期
- private int firstDayOfWeek = Calendar.SUNDAY;// 是星期日
- private int currentMonth = 0;
- private int currentYear = 0;
- private int displayWidth = 0;// 屏幕总宽度
- private int cell_Width = 0; // 日期单元格宽度
- private int cell_Height = 35; // 日期单元格高度
- // 页面控件
- TextView currentYAndM = null;
- Button preMonthButton = null;
- Button nextMonthButton = null;
- LinearLayout mainLayout = null;
- LinearLayout calendarLayout = null;
- LinearLayout contentLayout = null;
- TextView contentText = null;
- // 数据源
- Boolean[] msgs = null;
- Calendar startDate = null;//表格的第一天的日期
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.calendar_main);
- this.viewModel = new BlogViewModel(this);
- // 获得屏幕宽和高,并計算出屏幕寬度分七等份的大小
- WindowManager windowManager = getWindowManager();
- Display display = windowManager.getDefaultDisplay();
- displayWidth = display.getWidth();
- cell_Width = displayWidth / 7 + 1;
- // 制定布局文件,并设置属性
- mainLayout = (LinearLayout) this.findViewById(R.id.date_calender_framelayout);
- currentYAndM = (TextView) findViewById(R.id.Top_Date);
- preMonthButton = (Button) findViewById(R.id.btn_pre_month);
- nextMonthButton = (Button) findViewById(R.id.btn_next_month);
- preMonthButton.setOnClickListener(new OnClickPreMonthListener());
- nextMonthButton.setOnClickListener(new OnClickNextMonthListener());
- // 计算本月日历中的第一天(一般是上月的某天),并更新日历
- mFirstDateOfPanel = getCalendarStartDate();
- this.mTodayDate = getTodayDate();
- this.startDate = getStartDate();
- /*
- * 初始化日期视图
- * Calendar部分
- */
- View calendarView = generateCalendarView();
- this.mainLayout.addView(calendarView);
- //刷新日期视图
- this.refreshCalendar();
- /*
- * Description 部分
- */
- ScrollView view = new ScrollView(this);
- contentLayout = createLayout(LinearLayout.VERTICAL);
- contentLayout.setPadding(5, 2, 0, 0);
- contentText = new TextView(this);
- contentText.setTextColor(Color.BLACK);
- contentText.setTextSize(18);
- contentLayout.addView(contentText);
- LinearLayout.LayoutParams Param1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
- view.addView(contentLayout, Param1);
- mainLayout.setBackgroundColor(Color.WHITE);
- mainLayout.addView(view);
- /* 新建线程
- new Thread() {
- @Override
- public void run() {
- int day = getIndexFromDates(mTodayDate, startDate);
- Log.i("sys", "初始时 day = "+day);
- }
- }.start();*/
- Calendar_WeekBgColor = this.getResources().getColor(R.color.Calendar_WeekBgColor);
- Calendar_DayBgColor = this.getResources().getColor(R.color.Calendar_DayBgColor);
- IsHoliday_BgColor = this.getResources().getColor(R.color.isHoliday_BgColor);
- UnPresentMonth_FontColor = this.getResources().getColor(R.color.unPresentMonth_FontColor);
- IsPresentMonth_FontColor = this.getResources().getColor(R.color.isPresentMonth_FontColor);
- IsToday_BgColor = this.getResources().getColor(R.color.isToday_BgColor);
- Special_Reminder = this.getResources().getColor(R.color.specialReminder);
- Common_Reminder = this.getResources().getColor(R.color.commonReminder);
- Calendar_WeekFontColor = this.getResources().getColor(R.color.Calendar_WeekFontColor);
- }
- protected String getDateShortString(Calendar date) {
- String returnString = date.get(Calendar.YEAR) + "-";
- returnString += date.get(Calendar.MONTH) + 1 + "-";
- returnString += date.get(Calendar.DAY_OF_MONTH);
- return returnString;
- }
- /**
- * Return the Date's index of {[url=home.php?mod=space&uid=17823]@LINK[/url] returnDate} from {@link datesList};
- * First is Today's index
- *
- * @param now
- * today
- * @param returnDate
- * click date
- * @return
- */http://m.nvzi91.cn/gongjingfeida/29428.html
- private int getIndexFromDates(Calendar now, Calendar returnDate) {
- Calendar cNow = (Calendar) now.clone();
- Calendar cReturnDate = (Calendar) returnDate.clone();
- CalenderUtil.setTimeToMidnight(cNow);
- CalenderUtil.setTimeToMidnight(cReturnDate);
- long todayMs = cNow.getTimeInMillis();
- long returnMs = cReturnDate.getTimeInMillis();
- long intervalMs = todayMs - returnMs;
- int index = CalenderUtil.millisecondsToDays(intervalMs);
- Log.i("sys", "Index = " + index);
- return index;
- }
- /**
- * 生成日期视图 即初始化calendarLayout
- *
- * @return
- */
- private View generateCalendarView() {
- calendarLayout = createLayout(LinearLayout.VERTICAL);
- // layContent.setPadding(1, 0, 1, 0);
- calendarLayout.setBackgroundColor(Color.argb(255, 105, 105, 103));
- calendarLayout.addView(generateCalendarWeekRows());
- days.clear();
- for (int iRow = 0; iRow < 6; iRow++) {
- calendarLayout.addView(generateCalendarDayRows());
- }
- return calendarLayout;
- }
[Android教程] Android系统自定义实现日历控件
最新推荐文章于 2024-02-23 09:42:16 发布