package com.maxus.ndk.myapplication;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
/**
* Created by anxiaoniu 2022/0921
* 日历视图控件
*/
public class CalendarView extends LinearLayout {
/**
* 日期信息实体类
**/
public class DayInfo {
public int day;
public DayType dayType;
@Override
public String toString() {
return String.valueOf(day);
}
}
/**
* 日期类型
**/
public enum DayType {
DAY_TYPE_NONE(0),
DAY_TYPE_FORE(1),
DAY_TYPE_NOW(2),
DAY_TYPE_NEXT(3);
private int value;
DayType(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
private Context context;//上下文对象
private TextView txtTitle;//标题文字
private TextView tvYear;
private GridView dateGrid;//日期表格
private final Calendar calendar = Calendar.getInstance();
private static final int MAX_DAY_COUNT = 42;//最大格子数量
private DayInfo[] dayInfos = new DayInfo[MAX_DAY_COUNT];//每月应该有的天数,36为最大格子数
public CalendarView(Context context) {
super(context);
init(context);//初始化程序
showCalendar(calendar);//显示日历数据
}
public CalendarView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);//初始化程序
showCalendar(calendar);//显示日历数据
}
/**
* 初始化程序
**/
private void init(Context context) {
this.context = context;
View rootView = View.inflate(context, R.layout.widget_calendar, null);
dateGrid = rootView.findViewById(R.id.widgetCalendar_calendar);
txtTitle = rootView.findViewById(R.id.widgetCalendar_txtTitle);
tvYear = rootView.findViewById(R.id.tv_year);
rootView.findViewById(R.id.widgetCalendar_imgForeYear)
.setOnClickListener(navigatorClickListener);
rootView.findViewById(R.id.widgetCalendar_imgForeMonth)
.setOnClickListener(navigatorClickListener);
rootView.findViewById(R.id.widgetCalendar_imgNextMonth)
.setOnClickListener(navigatorClickListener);
rootView.findViewById(R.id.widgetCalendar_imgNextYear)
.setOnClickListener(navigatorClickListener);
this.setOrientation(VERTICAL);//设置布局方向
this.addView(rootView);//添加根视图
}
/**
* 显示日历数据
**/
private void showCalendar(Calendar calendar) {
int year = calendar.get(Calendar.YEAR);//获得年份
int month = calendar.get(Calendar.MONTH) + 1;//获取月份
// int day = calendar.get(Calendar.DATE);//获取天数
int centry = Integer.valueOf(String.valueOf(year).substring(0, 2));//取年份前两位作为世纪数,世纪数-1
int tmpYear = Integer.valueOf(String.valueOf(year).substring(2, 4));//取年份后两位
if (month == 1 || month == 2) {//该年的1、2月看作为前一年的13月,14月
tmpYear -= 1;
month += 12;
}
//计算该月的第一天是星期几
int firstOfWeek = (tmpYear + (tmpYear / 4) + centry / 4 - 2 * centry + 26 * (month + 1) / 10) % 7;
Log.d("wgx", "firstOfWeek=" + firstOfWeek);
/**
* 处理星期的显示
* */
if (firstOfWeek <= 0) {
firstOfWeek = 7 + firstOfWeek;
}
/**
* firstOfWeek可以显示从周一到周日
* firstOfWeek+1可以显示周日到周一
* */
firstOfWeek=firstOfWeek+1;
Log.d("wgx", "firstOfWeek=" + firstOfWeek);
//计算第一天所在的索引值,如果该天为星期一,则做换行处理
final int firstDayIndex = firstOfWeek == 1 ? 7 : firstOfWeek - 1;
final int dayCount = getDayCount(year, month);//获取该月的天数
//处理本月的数据
for (int i = firstDayIndex; i < firstDayIndex + dayCount; i++) {
if (dayInfos[i] == null)
dayInfos[i] = new DayInfo();
dayInfos[i].day = i - firstDayIndex + 1;
dayInfos[i].dayType = DayType.DAY_TYPE_NOW;
}
//处理前一个月的数据
calendar.add(Calendar.MONTH, -1);//前一个月
year = calendar.get(Calendar.YEAR);//获得年份
month = calendar.get(Calendar.MONTH) + 1;//获取月份
final int foreDayCount = getDayCount(year, month);//获得前一个月的天数
for (int i = 0; i < firstDayIndex; i++) {
if (dayInfos[i] == null)
dayInfos[i] = new DayInfo();
dayInfos[i].day = foreDayCount - firstDayIndex + i + 1;
dayInfos[i].dayType = DayType.DAY_TYPE_FORE;
}
//处理下一个月的数据
for (int i = 0; i < MAX_DAY_COUNT - dayCount - firstDayIndex; i++) {
if (dayInfos[firstDayIndex + dayCount + i] == null)
dayInfos[firstDayIndex + dayCount + i] = new DayInfo();
dayInfos[firstDayIndex + dayCount + i].day = i + 1;
dayInfos[firstDayIndex + dayCount + i].dayType = DayType.DAY_TYPE_NEXT;
}
calendar.add(Calendar.MONTH, 1);//还原月份数据
txtTitle.setText(new SimpleDateFormat("yyyy年MM月").format(calendar.getTime()));//设置日历显示的标题
dateGrid.setAdapter(new CalendarAdapter(context, dayInfos));
dateGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("wgx","position==="+position);
}
});
}
/**
* 导航按钮点击事件
**/
private OnClickListener navigatorClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.widgetCalendar_imgForeMonth://上一月
calendar.add(Calendar.MONTH, -1);
break;
case R.id.widgetCalendar_imgForeYear://上一年
calendar.add(Calendar.YEAR, -1);
break;
case R.id.widgetCalendar_imgNextMonth://下一月
calendar.add(Calendar.MONTH, 1);
break;
case R.id.widgetCalendar_imgNextYear://下一年
calendar.add(Calendar.YEAR, 1);
tvYear.setText(calendar.get(Calendar.YEAR) + "");
break;
}
tvYear.setText(calendar.get(Calendar.YEAR) + "");
showCalendar(calendar);//显示日历数据
}
};
/**
* 是否是平年
**/
private boolean isLeapYear(int year) {
return !((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}
/**
* 获取某年的某月有多少天
*
* @param year
* @param month
* @return
*/
private int getDayCount(int year, int month) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
case 13://其实�
没有合适的资源?快使用搜索试试~ 我知道了~
android 自定义日历选择器
共30个文件
png:16个
xml:12个
java:2个
需积分: 5 1 下载量 46 浏览量
2022-09-22
14:39:32
上传
评论
收藏 92KB RAR 举报
温馨提示
android 版本下,模仿手机日历app ,使用gridview 开发完成的日历
资源详情
资源评论
资源推荐
收起资源包目录
main.rar (30个子文件)
main
res
mipmap-xxxhdpi
ic_launcher.png 10KB
ic_launcher_round.png 16KB
drawable
date_choosed_bg.xml 199B
ic_launcher_background.xml 6KB
mipmap-hdpi
ic_launcher.png 4KB
ic_launcher_round.png 5KB
drawable-v24
ic_launcher_foreground.xml 2KB
values
colors.xml 387B
themes.xml 850B
strings.xml 78B
values-night
themes.xml 850B
mipmap-xxhdpi
ic_launcher.png 8KB
ic_launcher_round.png 12KB
mipmap-mdpi
ic_arrow_right.png 586B
ic_arrow_left.png 720B
ic_launcher.png 5KB
btn_lower.png 1KB
btn_left.png 1KB
btn_upper.png 1KB
btn_right.png 1KB
ic_launcher_round.png 3KB
mipmap-xhdpi
ic_launcher.png 5KB
ic_launcher_round.png 7KB
mipmap-anydpi-v26
ic_launcher_round.xml 276B
ic_launcher.xml 276B
layout
activity_main.xml 731B
widget_calendar.xml 6KB
java
com
maxus
ndk
myapplication
CalendarView.java 13KB
MainActivity.java 353B
AndroidManifest.xml 752B
共 30 条
- 1
安小牛
- 粉丝: 7
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 猫咪和汤58.apk
- 浏览器.apk
- 基于matlab实现是一个matlab中的power system 中搭建的一个模型
- 基于JSP毕业设计-教学管理系统(源代码+论文).zip
- 基于JSP毕业设计-家政管理系统-毕业设计.zip
- 基于Python实现淘宝商品评论采集(含逆向)源代码
- 基于matlab实现多目标进化算法NSGAⅡ&Matlab讲解.rar
- 基于matlab实现多车辆车辆路径问题,用遗传算法编程,保证可用.rar
- 基于matlab实现多层极限学习机实现手写体识别,准确率超过99%.rar
- 基于matlab实现电力系统最优潮流程序,可以应用于电力市场下的最优潮流计算 适合电力系统专业的同仁使用.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0