Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android使用GridView實現日歷的簡單功能

Android使用GridView實現日歷的簡單功能

編輯:關於Android編程

簡單的日歷實現,只是顯示了每一個月,沒有顯示當天和記事這些功能

主要是計算月初是周幾,月末是周幾,然後相應的顯示上一月多少天和下一月多少天。

先看一下關於日期的用到的幾個工具類

 /**
   * 獲取該月天數
   */
  public static int getCurrentMonthDay(long millSec) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(millSec);
    calendar.set(Calendar.DATE, 1);
    calendar.roll(Calendar.DATE, -1);
    int dateCount = calendar.get(Calendar.DATE);
    return dateCount;
  }

  /**
   * 獲取當月第一天
   */
  public static long getFirOfMonth(long millSec) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(millSec);
    calendar.set(Calendar.DATE, 1);
    return calendar.getTimeInMillis();
  }

  /**
   * 獲取當前時間戳
   */
  public static long getCurrentTime() {
    Calendar calendar = Calendar.getInstance();
    return calendar.getTimeInMillis();
  }

  /**
   * 獲取上一月/下一月
   */
  public static long getLastOrNextMonth(long millSec, int count) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(millSec);
    calendar.add(Calendar.MONTH, count);
    return calendar.getTimeInMillis();
  }

  /**
   * 格式化到月份
   */
  public static String long2str(long millSec) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    return sdf.format(new Date(millSec));
  }

用一個List存放每一天,因為之前考慮到每一天有記事這些,所以封裝了一個類,如果不需要的話可以直接一個時間戳就可以。
打開之後先獲取當前的時間戳,然後初始化到當前月份的第一天,通過工具類中方法。
然後就是計算了,把上月結余的、本月所有、下月的一起添加到List中。

 private void setCalendarList(long millSecs) {
    beans.clear();
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(millSecs);
//    int nor = today.getDay();
    int max = DateUtil.getCurrentMonthDay(millSecs);
    /**
     * 當月日歷
     */
    for (int i = 1; i <= max; i++) {
      DayBean bean = new DayBean();
      Calendar cc = Calendar.getInstance();
      cc.setTimeInMillis(millSecs);
      cc.add(Calendar.DAY_OF_MONTH, i - 1);
      setBean(bean, cc);
      bean.setIsCurrentMonth(true);
      beans.add(bean);
    }
    //上月結余
    int fir_day_of_week = beans.get(0).getCalendar().get(Calendar.DAY_OF_WEEK);
    Log.e("AAA", "week_last:" + fir_day_of_week);
    if (fir_day_of_week != 2) {
      if(fir_day_of_week == 1){
        for (int i = 0; i < 6; i++) {
          DayBean bean = new DayBean();
          Calendar cc = Calendar.getInstance();
          cc.setTimeInMillis(millSecs);
          cc.add(Calendar.DAY_OF_MONTH, -i -1);
          setBean(bean, cc);
          Log.e("AAA", "last:" + bean.getDay());
          bean.setIsCurrentMonth(false);
          beans.add(0, bean);
        }
      }else{
        for (int i = 0; i < fir_day_of_week-2; i++) {
          DayBean bean = new DayBean();
          Calendar cc = Calendar.getInstance();
          cc.setTimeInMillis(millSecs);
          cc.add(Calendar.DAY_OF_MONTH, -i-1);
          setBean(bean, cc);
          Log.e("AAA", "last:" + bean.getDay());
          bean.setIsCurrentMonth(false);
          beans.add(0, bean);
        }
      }

    }
    //下月
    int last_day_of_week = beans.get(beans.size() - 1).getCalendar().get(Calendar.DAY_OF_WEEK);
    Log.e("AAA", "week_next:" + last_day_of_week);
    if (last_day_of_week != 1) {
      for (int i = last_day_of_week; i < 8; i++) {
        DayBean bean = new DayBean();
        Calendar cc = Calendar.getInstance();
        cc.setTimeInMillis(millSecs);
        cc.add(Calendar.DAY_OF_MONTH, i - last_day_of_week);
        setBean(bean, cc);
        Log.e("AAA", "next:" + bean.getDay());
        bean.setIsCurrentMonth(false);
        beans.add(bean);
      }
    }
    GridViewAdapter adapter = new GridViewAdapter(this, beans);
    mGridView.setAdapter(adapter);
  }

  private void setBean(DayBean bean, Calendar cc) {
    bean.setCalendar(cc);
    bean.setDay(cc.get(Calendar.DAY_OF_MONTH));
  }

設置每一天時設置了一個字段為是否為本月,如果是,則設置為黑色,否則設為灰色顯示,在Adapter中的getView方法中設置即可。

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
      convertView = inflater.inflate(R.layout.item_layout, null);
      holder = new ViewHolder();
      holder.day_text = (TextView) convertView.findViewById(R.id.day_text);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    holder.day_text.setText(beans.get(position).getDay() + "");
    if (beans.get(position).isToday()) {
      holder.day_text.setBackgroundColor(Color.RED);
    } else {
      holder.day_text.setBackgroundColor(Color.WHITE);
    }
    if (beans.get(position).isCurrentMonth()) {
      holder.day_text.setTextColor(Color.BLACK);
    } else {
      holder.day_text.setTextColor(Color.GRAY);
    }

    return convertView;
  }

  class ViewHolder {
    TextView day_text;
  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

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