Android?Studio簡(jiǎn)單實(shí)現(xiàn)自定義日歷
本文實(shí)例為大家分享了Android Studio自定義日歷的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
目錄樹(shù)
1.DayBean.java用來(lái)存儲(chǔ)每天的信息
package com.example.l_b.calendar.bean; public class DayBean { ? ? private int day; ? ? private int month; ? ? private int year; ? ? // 是否為當(dāng)前月 ? ? 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; ? ? ? ? // 使用緩存機(jī)制提高利用率 ? ? ? ? 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 { ? ? ? ? ? ? // 通過(guò) parseColor 方法得到的顏色不可以簡(jiǎn)寫(xiě),必須寫(xiě)滿(mǎn)六位 ? ? ? ? ? ? textView.setBackgroundColor(Color.parseColor("#aaaaaa")); ? ? ? ? ? ? textView.setTextColor(Color.BLACK); ? ? ? ? } ? ? ? ? // 返回 view 或 textView 都行,因?yàn)槎际峭粋€(gè)對(duì)象 ? ? ? ? return textView; ? ? } }
這個(gè)子 view 比較簡(jiǎn)單,可以自行去做成自己想要的效果
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); ? ? ? ? // 拿到日歷對(duì)象,動(dòng)態(tài)設(shè)置時(shí)間 ? ? ? ? // 使用日歷對(duì)象可以幫我們避免一些問(wèn)題,如 月數(shù) 的臨界點(diǎn)問(wèn)題,到的 12 月是再加 1 的話(huà)會(huì)自動(dòng) ? ? ? ? // 幫我們加到下一年去,同理從 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); ? ? ? ? // 得到本月一號(hào)的星期索引 ? ? ? ? // 索引從 1 開(kāi)始,第一個(gè)為星期日,減1是為了與星期對(duì)齊,如星期一對(duì)應(yīng)索引1,星期二對(duì)應(yīng)索引二 ? ? ? ? calendar.set(Calendar.DAY_OF_MONTH, 1); ? ? ? ? int weekIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1; ? ? ? ? // 將日期設(shè)為上個(gè)月 ? ? ? ? calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); ? ? ? ? int preMonthDays = getMonth(calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.YEAR)); ? ? ? ? // 拿到上一個(gè)月的最后幾天的天數(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è)為當(dāng)月 ? ? ? ? calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1); ? ? ? ? int currentDays = getMonth(calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.YEAR)); ? ? ? ? // 拿到當(dāng)月的天數(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); ? ? ? ? ? ? // 當(dāng)前日期 ? ? ? ? ? ? String nowDate = getFormatTime("yyyy-M-d", Calendar.getInstance().getTime()); ? ? ? ? ? ? // 選擇的日期 ? ? ? ? ? ? String selectDate = getFormatTime("yyyy-M-", calendar.getTime()) + (i + 1); ? ? ? ? ? ? // 假如相等的話(huà),那么就是今天的日期了 ? ? ? ? ? ? if (nowDate.contentEquals(selectDate)) { ? ? ? ? ? ? ? ? bean.setCurrentDay(true); ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? bean.setCurrentDay(false); ? ? ? ? ? ? } ? ? ? ? ? ? bean.setCurrentMonth(true); ? ? ? ? ? ? dataList.add(bean); ? ? ? ? } ? ? ? ?? ? ? ? ? // 拿到下個(gè)月第一周的天數(shù) ? ? ? ? // 先拿到下個(gè)月第一天的星期索引 ? ? ? ? // 之前設(shè)為了1號(hào),所以將日歷對(duì)象的月數(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è)為當(dāng)月 ? ? ? ? calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); ? ? } ? ? // 設(shè)置當(dāng)前的時(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í)間,設(shè)置時(shí)間很方便,也比較簡(jiǎn)單,學(xué)的很快 ? ? public static String getFormatTime(String p, Date t) { ? ? ? ? return new SimpleDateFormat(p, Locale.CHINESE).format(t); ? ? } ? ? // 傳入年和月得出當(dāng)月的天數(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; ? ? ? ? } ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 一個(gè)日歷控件的實(shí)現(xiàn)代碼
- Android開(kāi)發(fā)之日歷CalendarView用法示例
- Android實(shí)現(xiàn)自定義日歷
- android 開(kāi)發(fā)教程之日歷項(xiàng)目實(shí)踐(一)
- Android自定義控件實(shí)現(xiàn)可多選課程日歷CalendarView
- android 開(kāi)發(fā)教程之日歷項(xiàng)目實(shí)踐(三)
- android 開(kāi)發(fā)教程之日歷項(xiàng)目實(shí)踐(二)
- Android可簽到日歷控件的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)日歷控件示例代碼
- Android 仿日歷翻頁(yè)、仿htc時(shí)鐘翻頁(yè)、數(shù)字翻頁(yè)切換效果
相關(guān)文章
Android實(shí)現(xiàn)搜索保存歷史記錄功能
這篇文章主要介紹了Android實(shí)現(xiàn)搜索保存歷史記錄功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Android自定義實(shí)現(xiàn)側(cè)滑菜單效果
這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)側(cè)滑菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Android Parcelable與Serializable詳解及區(qū)別
這篇文章主要介紹了Android Parcelable與Serializable詳解及區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-01-01Android中實(shí)現(xiàn)GPS定位的簡(jiǎn)單例子
這篇文章主要介紹了Android中實(shí)現(xiàn)GPS定位的簡(jiǎn)單例子,例子邏輯清晰,但相對(duì)簡(jiǎn)單了些,需要的朋友可以參考下2014-07-07Android ImageView實(shí)現(xiàn)圖片裁剪和顯示功能
這篇文章主要介紹了Android ImageView實(shí)現(xiàn)圖片裁剪和顯示功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02Java程序員轉(zhuǎn)Android開(kāi)發(fā)必讀經(jīng)驗(yàn)一份
小編最近幾日偷偷的發(fā)現(xiàn)部分Java程序員想轉(zhuǎn)安卓開(kāi)發(fā),故此加緊補(bǔ)充知識(shí),為大家搜集資料,積極整理前人的經(jīng)驗(yàn),希望可以給正處于困惑中的你,帶來(lái)些許的幫助。2017-11-11