广告
返回顶部
首页 > 资讯 > 移动开发 >Android Studio简单实现自定义日历
  • 475
分享到

Android Studio简单实现自定义日历

2024-04-02 19:04:59 475人浏览 泡泡鱼
摘要

本文实例为大家分享了Android Studio自定义日历的具体代码,供大家参考,具体内容如下 效果图: 目录树 1.DayBean.java用来存储每天的信息 package

本文实例为大家分享了Android Studio自定义日历的具体代码,供大家参考,具体内容如下

效果图:

目录树

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 可以帮我们快速规范代码

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);

        // 拿到日历对象,动态设置时间
        // 使用日历对象可以帮我们避免一些问题,如 月数 的临界点问题,到的 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是为了与星期对齐,如星期一对应索引1,星期二对应索引二
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        int weekIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1;


        // 将日期设为上个月
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
        int preMonthDays = getMonth(calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.YEAR));
        // 拿到上一个月的最后几天的天数
        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);
        }

        // 将日期设为当月
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
        int currentDays = getMonth(calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.YEAR));
        // 拿到当月的天数
        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);
        }
        
        // 拿到下个月第一周的天数
        // 先拿到下个月第一天的星期索引
        // 之前设为了1号,所以将日历对象的月数加 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();
        // 最后将日期设为当月
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
    }
    // 设置当前的时间
    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;
    }
    // 格式化时间,设置时间很方便,也比较简单,学的很快
    public static String getFormatTime(String p, Date t) {
        return new SimpleDateFormat(p, Locale.CHINESE).format(t);
    }
    // 传入年和月得出当月的天数
    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;
        }
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: Android Studio简单实现自定义日历

本文链接: https://www.lsjlt.com/news/148396.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android Studio简单实现自定义日历
    本文实例为大家分享了Android Studio自定义日历的具体代码,供大家参考,具体内容如下 效果图: 目录树 1.DayBean.java用来存储每天的信息 package ...
    99+
    2022-11-13
  • java简单实现自定义日历
    本文实例为大家分享了java自定义日历的具体代码,供大家参考,具体内容如下 效果图: 源码: package com.example; import java.text.Simp...
    99+
    2022-11-13
  • Android实现自定义日历
    自定义日历控件,支持旧历、节气、日期标注、点击操作 (参考网络上的日历控件改写) 注:将下面的四张资源图片拷贝到所建包的下一个image目录中,如Calendar.java 所...
    99+
    2022-06-06
    自定义 Android
  • Android自定义实现日历控件
    本文实例为大家分享了Android自定义实现日历控件的具体代码,供大家参考,具体内容如下 1. Calendar类 2. 布局 创建calendar_layout.xml <...
    99+
    2022-11-12
  • Android自定义日历Calender代码实现
    产品要做签到功能,签到功能要基于一个日历来进行,所以就根据 要求自定义了一个日历 自定义控件相信做android都知道: (1)首先创建一个类,继承一个容器类或者是一个控...
    99+
    2022-06-06
    Android
  • Android如何自定义实现日历控件
    这篇文章主要介绍Android如何自定义实现日历控件,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!具体内容如下1. Calendar类2. 布局创建calendar_layout.xml<LinearLayou...
    99+
    2023-06-25
  • Android自定义日历效果
    要实现Android自定义日历效果,可以按照以下步骤进行:1. 创建一个自定义的CalendarView控件,继承自ViewGrou...
    99+
    2023-08-15
    Android
  • Android自定义view实现日历打卡签到
    本文实例为大家分享了Android自定义view实现日历打卡签到的具体代码,供大家参考,具体内容如下 1.说明 自己写一个view实现每天签到的功能,设置背景图片 源码下载 2.效果...
    99+
    2022-11-12
  • android自定义view之实现日历界面实例
    现在网上有很多自定义view实现日历的demo,今天讲一讲如何自己实现这个自定义view。 看一下最终效果图: 在这个自定义view中,我使用了各种奇技淫巧的方法来实现这个日...
    99+
    2022-06-06
    view 界面 Android
  • Android简单实现自定义弹框(PopupWindow)
    一:一般都是先上效果图 二:实现步骤: 1.xml布局实现 <?xml version="1.0" encoding="utf-8"?> &...
    99+
    2022-06-06
    自定义 popupwindow Android
  • Android自定义dialog简单实现方法
    本文实例讲述了Android自定义dialog简单实现方法。分享给大家供大家参考,具体如下: @Override protected void onCreate(Bundl...
    99+
    2022-06-06
    方法 dialog Android
  • Android自定义日历控件实例详解
    为什么要自定义控件 有时,原生控件不能满足我们对于外观和功能的需求,这时候可以自定义控件来定制外观或功能;有时,原生控件可以通过复杂的编码实现想要的功能,这时候可以自定义控件来...
    99+
    2022-06-06
    Android
  • Android自定义对话框的简单实现
    本文实例为大家分享了Android自定义对话框的具体实现代码,供大家参考,具体内容如下 1、定义对话框的布局 <xml version="1.0" encoding="utf-...
    99+
    2022-11-13
  • Android怎么自定义日历效果
    要自定义Android日历效果,可以使用Android中的CalendarView或者自定义View来实现。以下是一种简单的自定义日...
    99+
    2023-08-16
    Android
  • Android如何自定义view实现日历打卡签到
    这篇文章主要介绍Android如何自定义view实现日历打卡签到,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!Android是什么Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备...
    99+
    2023-06-14
  • Android自定义对话框Dialog的简单实现
    本文着重研究了自定义对话框,通过一下步骤即可清晰的理解原理,通过更改界面设置和style类型,可以应用在各种各样适合自己的App中。首先来看一下效果图:首先是activity的界面点击了上述图片的按钮后,弹出对话框:点击对话框的确定按钮:点...
    99+
    2023-05-30
    android 对话框 dialog
  • Android使用GridView实现日历的简单功能
    简单的日历实现,只是显示了每一个月,没有显示当天和记事这些功能 主要是计算月初是周几,月末是周几,然后相应的显示上一月多少天和下一月多少天。 先看一下关于日期的用到的几个工具...
    99+
    2022-06-06
    gridview Android
  • android自定义View实现简单五子棋游戏
    做一个五子棋练练手,没什么特别的,再复习一下自定义View的知识,onMeasure,MeasureSpec , onDraw以及OnTouchEvent方法等。 效果图 代码如下...
    99+
    2022-11-13
  • Android实现简单的自定义ViewGroup流式布局
    目录前言一、基本的测量与布局二、流式的布局的layout三、流式的布局的Measure后记前言 前面几篇我们简单的复习了一下自定义 View 的测量与绘制,并且回顾了常见的一些事件的...
    99+
    2022-12-09
    Android自定义ViewGroup流式布局 Android ViewGroup流式布局 Android ViewGroup 布局
  • Android自定义控件简单实现侧滑菜单效果
    侧滑菜单在很多应用中都会见到,最近QQ5.0侧滑还玩了点花样~~对于侧滑菜单,一般大家都会自定义ViewGroup,然后隐藏菜单栏,当手指滑动时,通过Scroller或者不断的...
    99+
    2022-06-06
    菜单 Android
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作