Android?Studio簡單實現(xiàn)自定義日歷
本文實例為大家分享了Android Studio自定義日歷的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:

目錄樹

1.DayBean.java用來存儲每天的信息
package com.example.l_b.calendar.bean;
public class DayBean {
? ? private int day;
? ? private int month;
? ? private int year;
? ? // 是否為當前月
? ? private boolean currentMonth;
? ? // 是否為今天
? ? private boolean currentDay;
? ? public int getDay() {
? ? ? ? return day;
? ? }
? ? public void setDay(int day) {
? ? ? ? this.day = day;
? ? }
? ? public int getMonth() {
? ? ? ? return month;
? ? }
? ? public void setMonth(int month) {
? ? ? ? this.month = month;
? ? }
? ? public int getYear() {
? ? ? ? return year;
? ? }
? ? public void setYear(int year) {
? ? ? ? this.year = year;
? ? }
? ? public boolean isCurrentMonth() {
? ? ? ? return currentMonth;
? ? }
? ? public void setCurrentMonth(boolean currentMonth) {
? ? ? ? this.currentMonth = currentMonth;
? ? }
? ? public boolean isCurrentDay() {
? ? ? ? return currentDay;
? ? }
? ? public void setCurrentDay(boolean currentDay) {
? ? ? ? this.currentDay = currentDay;
? ? }
}2.自定義適配器
package com.example.l_b.calendar.adapter;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.l_b.calendar.bean.DayBean;
import java.util.List;
public class DayAdapter extends BaseAdapter {
? ? private List<DayBean> list;
? ? private Context context;
? ? public DayAdapter(List<DayBean> list, Context context) {
? ? ? ? this.list = list;
? ? ? ? this.context = context;
? ? }
? ? @Override
? ? public int getCount() {
? ? ? ? return list.size();
? ? }
? ? @Override
? ? public DayBean getItem(int position) {
? ? ? ? return list.get(position);
? ? }
? ? @Override
? ? public long getItemId(int position) {
? ? ? ? return 0;
? ? }
? ? @Override
? ? public View getView(int position, View view, ViewGroup parent) {
? ? ? ? TextView textView;
? ? ? ? // 使用緩存機制提高利用率
? ? ? ? if (view == null) {
? ? ? ? ? ? textView = new TextView(context);
? ? ? ? ? ? textView.setPadding(5, 5, 5, 5);
? ? ? ? ? ? view = textView;
? ? ? ? } else {
? ? ? ? ? ? textView = (TextView) view;
? ? ? ? }
? ? ? ? DayBean bean = getItem(position);
? ? ? ? textView.setText(bean.getDay() + "");
? ? ? ? textView.setGravity(Gravity.CENTER);
? ? ? ? textView.setTextColor(Color.BLACK);
? ? ? ? textView.setTypeface(Typeface.DEFAULT_BOLD);
? ? ? ? if (bean.isCurrentDay()) {
? ? ? ? ? ? textView.setBackgroundColor(Color.parseColor("#fd5f00"));
? ? ? ? ? ? textView.setTextColor(Color.WHITE);
? ? ? ? } else if (bean.isCurrentMonth()) {
? ? ? ? ? ? textView.setBackgroundColor(Color.WHITE);
? ? ? ? ? ? textView.setTextColor(Color.BLACK);
? ? ? ? } else {
? ? ? ? ? ? // 通過 parseColor 方法得到的顏色不可以簡寫,必須寫滿六位
? ? ? ? ? ? textView.setBackgroundColor(Color.parseColor("#aaaaaa"));
? ? ? ? ? ? textView.setTextColor(Color.BLACK);
? ? ? ? }
? ? ? ? // 返回 view 或 textView 都行,因為都是同一個對象
? ? ? ? return textView;
? ? }
}這個子 view 比較簡單,可以自行去做成自己想要的效果
3.主布局
效果圖

代碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:orientation="vertical" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent"> ? ? <TextView ? ? ? ? android:id="@+id/tvCurrentDate" ? ? ? ? android:text="2019-6-24" ? ? ? ? android:textSize="18sp" ? ? ? ? android:gravity="center" ? ? ? ? android:textStyle="bold" ? ? ? ? android:textColor="#fff" ? ? ? ? android:background="#fd5f00" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="40dp" /> ? ? <LinearLayout ? ? ? ? android:background="#f6f6e9" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content"> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/tvPreMonth" ? ? ? ? ? ? android:padding="5dp" ? ? ? ? ? ? android:text="上一月" ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:textSize="16sp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:textStyle="bold" ? ? ? ? ? ? android:textColor="#000" ? ? ? ? ? ? android:layout_height="wrap_content" /> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="1dp" ? ? ? ? ? ? android:background="#000" ? ? ? ? ? ? android:layout_height="match_parent" /> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/tvNextMonth" ? ? ? ? ? ? android:text="下一月" ? ? ? ? ? ? android:textStyle="bold" ? ? ? ? ? ? android:textColor="#000" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:textSize="16sp" ? ? ? ? ? ? android:padding="5dp" ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:layout_height="wrap_content" /> ? ? </LinearLayout> ? ? <TextView ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:background="#000" ? ? ? ? android:layout_height="1dp" /> ? ? <LinearLayout ? ? ? ? android:background="#000" ? ? ? ? android:padding="1dp" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content"> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_marginRight="1dp" ? ? ? ? ? ? android:background="#fff" ? ? ? ? ? ? android:text="周日" ? ? ? ? ? ? android:textStyle="bold" ? ? ? ? ? ? android:textColor="#000" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:textSize="16sp" ? ? ? ? ? ? android:padding="5dp" ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:layout_height="wrap_content" /> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_marginRight="1dp" ? ? ? ? ? ? android:background="#fff" ? ? ? ? ? ? android:text="周一" ? ? ? ? ? ? android:textStyle="bold" ? ? ? ? ? ? android:textColor="#000" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:textSize="16sp" ? ? ? ? ? ? android:padding="5dp" ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:layout_height="wrap_content" /> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_marginRight="1dp" ? ? ? ? ? ? android:background="#fff" ? ? ? ? ? ? android:text="周二" ? ? ? ? ? ? android:textStyle="bold" ? ? ? ? ? ? android:textColor="#000" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:textSize="16sp" ? ? ? ? ? ? android:padding="5dp" ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:layout_height="wrap_content" /> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_marginRight="1dp" ? ? ? ? ? ? android:background="#fff" ? ? ? ? ? ? android:text="周三" ? ? ? ? ? ? android:textStyle="bold" ? ? ? ? ? ? android:textColor="#000" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:textSize="16sp" ? ? ? ? ? ? android:padding="5dp" ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:layout_height="wrap_content" /> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_marginRight="1dp" ? ? ? ? ? ? android:background="#fff" ? ? ? ? ? ? android:text="周四" ? ? ? ? ? ? android:textStyle="bold" ? ? ? ? ? ? android:textColor="#000" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:textSize="16sp" ? ? ? ? ? ? android:padding="5dp" ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:layout_height="wrap_content" /> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_marginRight="1dp" ? ? ? ? ? ? android:background="#fff" ? ? ? ? ? ? android:text="周五" ? ? ? ? ? ? android:textStyle="bold" ? ? ? ? ? ? android:textColor="#000" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:textSize="16sp" ? ? ? ? ? ? android:padding="5dp" ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:layout_height="wrap_content" /> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_marginRight="1dp" ? ? ? ? ? ? android:background="#fff" ? ? ? ? ? ? android:text="周六" ? ? ? ? ? ? android:textStyle="bold" ? ? ? ? ? ? android:textColor="#000" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:textSize="16sp" ? ? ? ? ? ? android:padding="5dp" ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:layout_height="wrap_content" /> ? ? </LinearLayout> ? ? <GridView ? ? ? ? android:id="@+id/gv" ? ? ? ? android:numColumns="7" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content"> ? ? </GridView> </LinearLayout>
tip:使用 ctrl + alt + 字母L 可以幫我們快速規(guī)范代碼
4.主代碼
package com.example.l_b.calendar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.GridView;
import android.widget.TextView;
import com.example.l_b.calendar.adapter.DayAdapter;
import com.example.l_b.calendar.bean.DayBean;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
? ? private TextView tvCurrentDate;
? ? private TextView tvPreMonth;
? ? private TextView tvNextMonth;
? ? private GridView gv;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? // 初始化布局
? ? ? ? initView();
? ? }
? ? private void initView() {
? ? ? ? tvCurrentDate = (TextView) findViewById(R.id.tvCurrentDate);
? ? ? ? tvPreMonth = (TextView) findViewById(R.id.tvPreMonth);
? ? ? ? tvNextMonth = (TextView) findViewById(R.id.tvNextMonth);
? ? ? ? gv = (GridView) findViewById(R.id.gv);
? ? ? ? // 初始化適配器
? ? ? ? initAdapter();
? ? }
? ? private void initAdapter() {
? ? ? ? final List<DayBean> dataList = new ArrayList<>();
? ? ? ? final DayAdapter adapter = new DayAdapter(dataList, this);
? ? ? ? gv.setAdapter(adapter);
? ? ? ? // 拿到日歷對象,動態(tài)設(shè)置時間
? ? ? ? // 使用日歷對象可以幫我們避免一些問題,如 月數(shù) 的臨界點問題,到的 12 月是再加 1 的話會自動
? ? ? ? // 幫我們加到下一年去,同理從 1 月到 12 月也一樣。
? ? ? ? final Calendar calendar = Calendar.getInstance();
? ? ? ? setCurrentData(calendar);
? ? ? ? updateAdapter(calendar, dataList, adapter);
? ? ? ? tvPreMonth.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
? ? ? ? ? ? ? ? updateAdapter(calendar, dataList, adapter);
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? tvNextMonth.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
? ? ? ? ? ? ? ? updateAdapter(calendar, dataList, adapter);
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? private void updateAdapter(Calendar calendar, List<DayBean> dataList, DayAdapter adapter) {
? ? ? ? dataList.clear();
? ? ? ? setCurrentData(calendar);
? ? ? ? // 得到本月一號的星期索引
? ? ? ? // 索引從 1 開始,第一個為星期日,減1是為了與星期對齊,如星期一對應(yīng)索引1,星期二對應(yīng)索引二
? ? ? ? calendar.set(Calendar.DAY_OF_MONTH, 1);
? ? ? ? int weekIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1;
? ? ? ? // 將日期設(shè)為上個月
? ? ? ? calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
? ? ? ? int preMonthDays = getMonth(calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.YEAR));
? ? ? ? // 拿到上一個月的最后幾天的天數(shù)
? ? ? ? for (int i = 0; i < weekIndex; i++) {
? ? ? ? ? ? DayBean bean = new DayBean();
? ? ? ? ? ? bean.setYear(calendar.get(Calendar.YEAR));
? ? ? ? ? ? bean.setMonth(calendar.get(Calendar.MONTH) + 1);
? ? ? ? ? ? bean.setDay(preMonthDays - weekIndex + i + 1);
? ? ? ? ? ? bean.setCurrentDay(false);
? ? ? ? ? ? bean.setCurrentMonth(false);
? ? ? ? ? ? dataList.add(bean);
? ? ? ? }
? ? ? ? // 將日期設(shè)為當月
? ? ? ? calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
? ? ? ? int currentDays = getMonth(calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.YEAR));
? ? ? ? // 拿到當月的天數(shù)
? ? ? ? for (int i = 0; i < currentDays; i++) {
? ? ? ? ? ? DayBean bean = new DayBean();
? ? ? ? ? ? bean.setYear(calendar.get(Calendar.YEAR));
? ? ? ? ? ? bean.setMonth(calendar.get(Calendar.MONTH) + 1);
? ? ? ? ? ? bean.setDay(i + 1);
? ? ? ? ? ? // 當前日期
? ? ? ? ? ? String nowDate = getFormatTime("yyyy-M-d", Calendar.getInstance().getTime());
? ? ? ? ? ? // 選擇的日期
? ? ? ? ? ? String selectDate = getFormatTime("yyyy-M-", calendar.getTime()) + (i + 1);
? ? ? ? ? ? // 假如相等的話,那么就是今天的日期了
? ? ? ? ? ? if (nowDate.contentEquals(selectDate)) {
? ? ? ? ? ? ? ? bean.setCurrentDay(true);
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? bean.setCurrentDay(false);
? ? ? ? ? ? }
? ? ? ? ? ? bean.setCurrentMonth(true);
? ? ? ? ? ? dataList.add(bean);
? ? ? ? }
? ? ? ??
? ? ? ? // 拿到下個月第一周的天數(shù)
? ? ? ? // 先拿到下個月第一天的星期索引
? ? ? ? // 之前設(shè)為了1號,所以將日歷對象的月數(shù)加 1 就行了
? ? ? ? calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
? ? ? ? weekIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1;
? ? ? ? for (int i = 0; i < 7 - weekIndex; i++) {
? ? ? ? ? ? DayBean bean = new DayBean();
? ? ? ? ? ? bean.setYear(calendar.get(Calendar.YEAR));
? ? ? ? ? ? bean.setMonth(calendar.get(Calendar.MONTH) + 1);
? ? ? ? ? ? bean.setDay(i + 1);
? ? ? ? ? ? bean.setCurrentDay(false);
? ? ? ? ? ? bean.setCurrentMonth(false);
? ? ? ? ? ? dataList.add(bean);
? ? ? ? }
? ? ? ? adapter.notifyDataSetChanged();
? ? ? ? // 最后將日期設(shè)為當月
? ? ? ? calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
? ? }
? ? // 設(shè)置當前的時間
? ? private void setCurrentData(Calendar calendar) {
? ? ? ? tvCurrentDate.setText(calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) + 1) + "月");
? ? }
? ? // 判斷是否為閏年
? ? public boolean isRunYear(int y) {
? ? ? ? return y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
? ? }
? ? // 格式化時間,設(shè)置時間很方便,也比較簡單,學(xué)的很快
? ? public static String getFormatTime(String p, Date t) {
? ? ? ? return new SimpleDateFormat(p, Locale.CHINESE).format(t);
? ? }
? ? // 傳入年和月得出當月的天數(shù)
? ? public int getMonth(int m, int y) {
? ? ? ? switch (m) {
? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ? return isRunYear(y) ? 29 : 28;
? ? ? ? ? ? case 4:
? ? ? ? ? ? case 6:
? ? ? ? ? ? case 9:
? ? ? ? ? ? case 11:
? ? ? ? ? ? ? ? return 30;
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? return 31;
? ? ? ? }
? ? }
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Parcelable與Serializable詳解及區(qū)別
這篇文章主要介紹了Android Parcelable與Serializable詳解及區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android ImageView實現(xiàn)圖片裁剪和顯示功能
這篇文章主要介紹了Android ImageView實現(xiàn)圖片裁剪和顯示功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02
Java程序員轉(zhuǎn)Android開發(fā)必讀經(jīng)驗一份
小編最近幾日偷偷的發(fā)現(xiàn)部分Java程序員想轉(zhuǎn)安卓開發(fā),故此加緊補充知識,為大家搜集資料,積極整理前人的經(jīng)驗,希望可以給正處于困惑中的你,帶來些許的幫助。2017-11-11

