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

安卓簡單日歷的實現

編輯:關於Android編程

最近無聊在做一個項目,需要用到日歷。日歷並不需要太復雜,只需要能夠簡單的查看就行,所以我選用了java提供的Calendar類,和安卓的gridView組合的形式.

先看效果圖:

\

背景什麼的先不要管他。只看主體部分,左箭頭查看上個月的,右箭頭查看下個月的.

接下來開始我們的日歷。

首先,需要使用Calendar自己構造一個DateUtils類,這個相當簡單。

import java.util.Calendar;

/**
 * Created by 91905 on 2016/8/22 0022.
 */

public class DateUtils {
    /**
     * 獲取當前年份
     *
     * @return
     */
    public static int getYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    /**
     * 獲取當前月份
     *
     * @return
     */
    public static int getMonth() {
        return Calendar.getInstance().get(Calendar.MONTH) + 1;
    }

    /**
     * 獲取當前日期是該月的第幾天
     *
     * @return
     */
    public static int getCurrentDayOfMonth() {
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 獲取當前日期是該周的第幾天
     *
     * @return
     */
    public static int getCurrentDayOfWeek() {
        return Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 獲取當前是幾點
     */
    public static int getHour() {
        return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);//二十四小時制
    }

    /**
     * 獲取當前是幾分
     *
     * @return
     */
    public static int getMinute() {
        return Calendar.getInstance().get(Calendar.MINUTE);
    }

    /**
     * 獲取當前秒
     *
     * @return
     */
    public static int getSecond() {
        return Calendar.getInstance().get(Calendar.SECOND);
    }

    /**
     * 根據傳入的年份和月份,獲取當前月份的日歷分布
     *
     * @param year
     * @param month
     * @return
     */
    public static int[][] getDayOfMonthFormat(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1);//設置時間為每月的第一天
        //設置日歷格式數組,6行7列
        int days[][] = new int[6][7];
        //設置該月的第一天是周幾
        int daysOfFirstWeek = calendar.get(Calendar.DAY_OF_WEEK);
        //設置本月有多少天
        int daysOfMonth = getDaysOfMonth(year, month);
        //設置上個月有多少天
        int daysOfLastMonth = getLastDaysOfMonth(year, month);
        int dayNum = 1;
        int nextDayNum = 1;
        //將日期格式填充數組
        for (int i = 0; i < days.length; i++) {
            for (int j = 0; j < days[i].length; j++) {
                if (i == 0 && j < daysOfFirstWeek - 1) {
                    days[i][j] = daysOfLastMonth - daysOfFirstWeek + 2 + j;
                } else if (dayNum <= daysOfMonth) {
                    days[i][j] = dayNum++;
                } else {
                    days[i][j] = nextDayNum++;
                }
            }
        }
        return days;
    }

    /**
     * 根據傳入的年份和月份,判斷上一個有多少天
     *
     * @param year
     * @param month
     * @return
     */
    public static int getLastDaysOfMonth(int year, int month) {
        int lastDaysOfMonth = 0;
        if (month == 1) {
            lastDaysOfMonth = getDaysOfMonth(year - 1, 12);
        } else {
            lastDaysOfMonth = getDaysOfMonth(year, month - 1);
        }
        return lastDaysOfMonth;
    }

    /**
     * 根據傳入的年份和月份,判斷當前月有多少天
     *
     * @param year
     * @param month
     * @return
     */
    public static int getDaysOfMonth(int year, int month) {
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 2:
                if (isLeap(year)) {
                    return 29;
                } else {
                    return 28;
                }
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
        }
        return -1;
    }

    /**
     * 判斷是否為閏年
     *
     * @param year
     * @return
     */
    public static boolean isLeap(int year) {
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            return true;
        }
        return false;
    }
}

其中DateUtils類中的

public static int[][] getDayOfMonthFormat(int year, int month) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month - 1, 1);//設置時間為每月的第一天
    //設置日歷格式數組,6行7列
    int days[][] = new int[6][7];
    //設置該月的第一天是周幾
    int daysOfFirstWeek = calendar.get(Calendar.DAY_OF_WEEK);
    //設置本月有多少天
    int daysOfMonth = getDaysOfMonth(year, month);
    //設置上個月有多少天
    int daysOfLastMonth = getLastDaysOfMonth(year, month);
    int dayNum = 1;
    int nextDayNum = 1;
    //將日期格式填充數組
    for (int i = 0; i < days.length; i++) {
        for (int j = 0; j < days[i].length; j++) {
            if (i == 0 && j < daysOfFirstWeek - 1) {
                days[i][j] = daysOfLastMonth - daysOfFirstWeek + 2 + j;
            } else if (dayNum <= daysOfMonth) {
                days[i][j] = dayNum++;
            } else {
                days[i][j] = nextDayNum++;
            }
        }
    }
    return days;
}

是將當前月份的日期分布保存到數組中返回.

接下來,布局文件



        

            

        

        

            
        

        

            
        
    

    

        

            

                
            

            

                
            

            

                
            

            

                
            

            

                
            

            

                
            

            

                
            
        

        
    


GridView的item文件




    


GridView的Adapter文件

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jiaoml.smallmusic.MusicApp;
import com.jiaoml.smallmusic.R;
import com.jiaoml.smallmusic.music.Record;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;

/**
 * Created by 91905 on 2016/8/24 0024.
 */

public class DateAdapter extends BaseAdapter {
    private int[] days = new int[42];
    private Context context;
    private int year;
    private int month;

    public DateAdapter(Context context, int[][] days, int year, int month) {
        this.context = context;
        int dayNum = 0;
	//將二維數組轉化為一維數組,方便使用
        for (int i = 0; i < days.length; i++) {
            for (int j = 0; j < days[i].length; j++) {
                this.days[dayNum] = days[i][j];
                dayNum++;
            }
        }
        this.year = year;
        this.month = month;
    }

    @Override
    public int getCount() {
        return days.length;
    }

    @Override
    public Object getItem(int i) {
        return days[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.date_item, null);
            viewHolder = new ViewHolder();
            viewHolder.date_item = (TextView) view.findViewById(R.id.date_item);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        if (i < 7 && days[i] > 20) {
            viewHolder.date_item.setTextColor(Color.rgb(204, 204, 204));//將上個月的和下個月的設置為灰色
        } else if (i > 20 && days[i] < 15) {
            viewHolder.date_item.setTextColor(Color.rgb(204, 204, 204));
        }
	viewHolder.date_item.setText(days[i] + "");

        return view;
    }

    /**
     * 優化Adapter
     */
    class ViewHolder {
        TextView date_item;
    }


接下來是Activity文件

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;


import com.jiaoml.smallmusic.R;
import com.jiaoml.smallmusic.adapter.DateAdapter;

/**
 * Created by 91905 on 2016/8/24 0024.
 */

public class MainActivity extends Activity implements View.OnClickListener {
    private GridView record_gridView;//定義gridView
    private DateAdapter dateAdapter;//定義adapter
    private ImageView record_left;//左箭頭
    private ImageView record_right;//右箭頭
    private TextView record_title;//標題
    private int year;
    private int month;
    private String title;
    private int[][] days = new int[6][7];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化日期數據
        initData();
        //初始化組件
        initView();
        //組件點擊監聽事件
        initEvent();
    }

    private void initData() {
        year = DateUtils.getYear();
        month = DateUtils.getMonth();
    }

    private void initEvent() {
        record_left.setOnClickListener(this);
        record_right.setOnClickListener(this);
    }

    private void initView() {
        /**
         * 以下是初始化GridView
         */
        record_gridView = (GridView) findViewById(R.id.record_gridView);
        days = DateUtils.getDayOfMonthFormat(2016, 8);
        dateAdapter = new DateAdapter(this, days, year, month);//傳入當前月的年
        record_gridView.setAdapter(dateAdapter);
        record_gridView.setVerticalSpacing(60);
        record_gridView.setEnabled(false);
        /**
         * 以下是初始化其他組件
         */
        record_left = (ImageView) findViewById(R.id.record_left);
        record_right = (ImageView) findViewById(R.id.record_right);
        record_title = (TextView) findViewById(R.id.record_title);
    }


    /**
     * 下一個月
     *
     * @return
     */
    private int[][] nextMonth() {
        if (month == 12) {
            month = 1;
            year++;
        } else {
            month++;
        }
        days = DateUtils.getDayOfMonthFormat(year, month);
        return days;
    }

    /**
     * 上一個月
     *
     * @return
     */
    private int[][] prevMonth() {
        if (month == 1) {
            month = 12;
            year--;
        } else {
            month--;
        }
        days = DateUtils.getDayOfMonthFormat(year, month);
        return days;
    }

    /**
     * 設置標題
     */
    private void setTile() {
        title = year + "年" + month + "月";
        record_title.setText(title);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.record_left:
                days = prevMonth();
                dateAdapter = new DateAdapter(this, days, year, month);
                record_gridView.setAdapter(dateAdapter);
                dateAdapter.notifyDataSetChanged();
                setTile();
                break;
            case R.id.record_right:
                days = nextMonth();
                dateAdapter = new DateAdapter(this, days, year, month);
                record_gridView.setAdapter(dateAdapter);
                dateAdapter.notifyDataSetChanged();
                setTile();
                break;
        }
    }
}

OK,一個簡單的日歷就完成了

 

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