1、效果图:

更新日志:2020-3-17(由于自己的疏忽导致布局文件没有上传,非常抱歉。)
dialog_date.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="15dp"android:background="@drawable/dialog_shape"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:layout_marginBottom="10dp"android:gravity="center"android:orientation="horizontal"><Buttonandroid:id="@+id/last_month"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="上个月" /><TextViewandroid:id="@+id/date_text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="15dp"android:layout_marginRight="15dp"android:text="2019年9月"android:textColor="@color/colorAccent"android:textSize="18sp" /><Buttonandroid:id="@+id/next_month"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="下个月" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="left"android:text="周日" /><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="周一" /><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="周二" /><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="周三" /><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="周四" /><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="周五" /><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="right"android:text="周六" /></LinearLayout><android.support.v7.widget.RecyclerViewandroid:id="@+id/recycler_select_date"android:layout_width="match_parent"android:layout_height="wrap_content" /><Viewandroid:layout_marginTop="10dp"android:layout_width="match_parent"android:layout_height="0.5dp"android:background="#cdcdcd" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"android:orientation="horizontal"><TextViewandroid:id="@+id/middle_cancel"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="0dp"android:layout_weight="1"android:gravity="center"android:padding="15dp"android:text="取消" /><Viewandroid:layout_width="0.5dp"android:layout_height="match_parent"android:background="#cdcdcd" /><TextViewandroid:id="@+id/middle_determine"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="0dp"android:layout_weight="1"android:gravity="center"android:padding="15dp"android:text="确定" /></LinearLayout></LinearLayout>
</LinearLayout>

2:、弹窗Dialog:SelectDateDialog:

public class SelectDateDialog {private static final String TAG = "SelectDateDialog";private Dialog dialog;private TextView dateText;private int selectYear, selectMonth;private AppCompatActivity mContext;private DateAdapter adapter;private List<String> selWeekList = new ArrayList<>();private List<DateBean> list = new ArrayList<>();public SelectDateDialog builder(AppCompatActivity mContext, int year, int month) {this.mContext = mContext;this.selectYear = year;this.selectMonth = month;// 获取Dialog布局View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_date, null);// 定义Dialog布局和参数dialog = new Dialog(mContext, R.style.AlertDialogStyle);dialog.setCanceledOnTouchOutside(false);//点击外部是否取消dialog.setCancelable(false);dialog.setContentView(view);Window window = dialog.getWindow();WindowManager.LayoutParams params = window.getAttributes();params.width = (ScreenUtils.getScreenWidth(mContext));
//        params.height = (int) (ScreenUtils.getScreenHeight(mContext) * 0.5);window.setAttributes(params);window.setGravity(Gravity.BOTTOM);RecyclerView recycler = view.findViewById(R.id.recycler_select_date);dateText = view.findViewById(R.id.date_text);dateText.setText(year + "年" + month + "月");//下个月view.findViewById(R.id.next_month).setOnClickListener(view13 -> {if (selectMonth > 11) {selectYear = selectYear + 1;selectMonth = 1;} else {selectMonth++;}showNewData(selectYear, selectMonth);});//上个月view.findViewById(R.id.last_month).setOnClickListener(view14 -> {if (selectMonth < 2) {selectYear = selectYear - 1;selectMonth = 12;} else {selectMonth--;}showNewData(selectYear, selectMonth);});list = DataUtils.getCalendar(year, month);adapter = new DateAdapter(mContext, list);GridLayoutManager manager = new GridLayoutManager(mContext, 7);recycler.setLayoutManager(manager);recycler.setAdapter(adapter);//取消view.findViewById(R.id.middle_cancel).setOnClickListener(view1 -> {dialog.dismiss();});//确定view.findViewById(R.id.middle_determine).setOnClickListener(view1 -> {initSelect();});//设置选中当天adapter.setNowDay(year, month, DataUtils.getLastMonth(year, month));return this;}private void showNewData(int year, int month) {list = DataUtils.getCalendar(year, month);//更新月数据adapter.setList(list);adapter.setNowDay(year , month, DataUtils.getLastMonth(year, month));dateText.setText(year + "年" + month + "月");}/*** 获取选中的日期*/private void initSelect() {selWeekList.clear();for (int i = 0; i < list.size(); i++) {if (list.get(i).isFlag()) {//记录数量selWeekList.add(selectYear + "-" + selectMonth + "-" + list.get(i).getWeek());}}Log.e(TAG, "initSelect: "+ list.size());Log.e(TAG, "initSelect: "+ selWeekList.size());if (selWeekList.size() == 0) {Toast.makeText(mContext, "未选则日期", Toast.LENGTH_SHORT).show();return;}dialog.dismiss();listener.date(DataUtils.returnList(selWeekList));Log.e(TAG, "initSelect: " + DataUtils.returnList(selWeekList));}public void show() {if (dialog != null && (!dialog.isShowing())) {dialog.show();}}private OnDateListener listener;public SelectDateDialog setListener(OnDateListener listener) {this.listener = listener;return this;}public interface OnDateListener {void date(String selectDate);}}

ScreenUtils:

public class ScreenUtils {public static int getScreenWidth(Context context) {DisplayMetrics localDisplayMetrics = new DisplayMetrics();((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);return localDisplayMetrics.widthPixels;}
}

DateBean:

public class DateBean {private int week;private boolean sign;//0 上月 1本月 2下月private int month;private boolean isFlag;public boolean isFlag() {return isFlag;}public void setFlag(boolean flag) {isFlag = flag;}public int getWeek() {return week;}public void setWeek(int week) {this.week = week;}public boolean isSign() {return sign;}public void setSign(boolean sign) {this.sign = sign;}public int getMonth() {return month;}public void setMonth(int month) {this.month = month;}public DateBean(int week, boolean sign, int month) {this.week = week;this.sign = sign;this.month = month;}
}

适配器:DateAdapter

public class DateAdapter extends CommonRecyclerAdapter<DateBean> {private static final String TAG = "DateAdapter";private Integer nowDay;private int year;private int month;public DateAdapter(Context context, List<DateBean> list) {super(context, R.layout.item_date, list);}public void setNowDay(int year, int month, int nowDay) {this.nowDay = nowDay;this.year = year;this.month = month;notifyDataSetChanged();}@Overridepublic void convert(RecyclerHolder holder, DateBean item, int position) {TextView number = holder.getView(R.id.item_number);number.setText(item.getWeek() + "");//当前年月等于切换年月时才选中当天if (position == nowDay) {String date = year + "-" + month;if (date.equals(DataUtils.timeInMillsTrasToDate(1))) {number.setBackgroundResource(R.drawable.date_unsel_shape);number.setTextColor(Color.WHITE);}else {number.setTextColor(Color.parseColor("#333333"));}} else {number.setBackgroundResource(0);number.setTextColor(Color.parseColor("#333333"));if (item.getMonth() == 0 || item.getMonth() == 2) {number.setTextColor(Color.parseColor("#CDCDCD"));} else {number.setTextColor(Color.parseColor("#333333"));}}//点击事件number.setOnClickListener(view -> {//本月可以点击int nowYear = Integer.parseInt(DataUtils.timeInMillsTrasToDate(2));int nowMonth = Integer.parseInt(DataUtils.timeInMillsTrasToDate(3));//只有在今天之后的日期才可以选中if (year == nowYear) {if (item.getMonth() == 1 && month == nowMonth && position > nowDay) {onClick(item, number);} else if (month > nowMonth) {onClick(item, number);}} else if (year > nowYear) {onClick(item, number);}});}private void onClick(DateBean item, TextView number) {if (item.isFlag()) {item.setFlag(false);number.setBackgroundResource(0);number.setTextColor(Color.parseColor("#333333"));} else {item.setFlag(true);number.setBackgroundResource(R.drawable.date_sel_shape);number.setTextColor(Color.WHITE);}}
}

注意:CommonRecyclerAdapter之前写过,这里不再重复

数据源:DataUtils

public class DataUtils {private static final String TAG = "DataUtils";/*** 日历*/public static List<DateBean> getCalendar(int currentYear, int currentMonth) {//实例化集合List<DateBean> list = new ArrayList<>();Calendar c = Calendar.getInstance();c.clear();/*** 处理上个月月末*/if (currentMonth - 1 == 0) {c.set(Calendar.YEAR, currentYear - 1);c.set(Calendar.MONTH, 11);} else {c.set(Calendar.YEAR, currentYear);c.set(Calendar.MONTH, (currentMonth - 2));}int last_sumDays = c.getActualMaximum(Calendar.DAY_OF_MONTH);c.set(Calendar.DATE, last_sumDays);//得到一号星期几int weekDay = c.get(Calendar.DAY_OF_WEEK);if (weekDay < 7) {for (int i = weekDay - 1; i >= 0; i--) {Integer date = new Integer(last_sumDays - i);list.add(new DateBean(date, true, 0));}}/*** 处理当前月*/c.clear();c.set(Calendar.YEAR, currentYear);c.set(Calendar.MONTH, currentMonth - 1);int currentsumDays = c.getActualMaximum(Calendar.DAY_OF_MONTH);for (int i = 1; i <= currentsumDays; i++) {Integer date = new Integer(i);list.add(new DateBean(date, true, 1));}/*** 处理下个月月初*/c.clear();if (currentMonth == 12) {c.set(Calendar.YEAR, currentYear + 1);c.set(Calendar.MONTH, 0);} else {c.set(Calendar.YEAR, currentYear);c.set(Calendar.MONTH, currentMonth);}c.set(Calendar.DATE, 1);int currentWeekDay = c.get(Calendar.DAY_OF_WEEK);if (currentWeekDay > 2 && currentWeekDay < 8) {for (int i = 1; i <= 8 - currentWeekDay; i++) {list.add(new DateBean(i, true, 2));}}return list;}/*** 获取上个月大小*/public static Integer getLastMonth(int currentYear, int currentMonth) {//实例化集合List<Integer> list = new ArrayList<>();Calendar c = Calendar.getInstance();c.clear();/*** 处理上个月月末*/if (currentMonth - 1 == 0) {c.set(Calendar.YEAR, currentYear - 1);c.set(Calendar.MONTH, 11);} else {c.set(Calendar.YEAR, currentYear);c.set(Calendar.MONTH, (currentMonth - 2));}int last_sumDays = c.getActualMaximum(Calendar.DAY_OF_MONTH);c.set(Calendar.DATE, last_sumDays);//得到一号星期几int weekDay = c.get(Calendar.DAY_OF_WEEK);if (weekDay < 7) {for (int i = weekDay - 1; i >= 0; i--) {list.add(i);}}Log.e(TAG, "getLastMonth: " + Integer.parseInt(timeInMillsTrasToDate(0)));return list.size() + Integer.parseInt(timeInMillsTrasToDate(0)) - 1;}/*** list转string字符串*/public static String returnList(List<String> list) {if (null == list && list.size() == 0) {return "";} else {//去除空格String str = String.valueOf(list).replaceAll(" ", "");return str.substring(1, str.length() - 1);}}/*** 时间转换*/@TargetApi(Build.VERSION_CODES.N)public static String timeInMillsTrasToDate(int formatType) {DateFormat formatter = null;switch (formatType) {case 0:formatter = new SimpleDateFormat("dd");break;case 1:formatter = new SimpleDateFormat("yyyy-MM");break;case 2:formatter = new SimpleDateFormat("yyyy");break;case 3:formatter = new SimpleDateFormat("MM");break;}Calendar calendar = Calendar.getInstance();return formatter.format(calendar.getTime());}
}

使用:DateActivity

public class DateActivity extends AppCompatActivity {private Button openDate;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_date);openDate = this.findViewById(R.id.open_date);openDate.setOnClickListener(view -> {showDateDialog();});}private void showDateDialog() {int year = Integer.parseInt(DataUtils.timeInMillsTrasToDate(2));int month = Integer.parseInt(DataUtils.timeInMillsTrasToDate(3));new SelectDateDialog().builder(this, year, month).setListener(selectDate -> openDate.setText(selectDate)).show();}
}

对应布局:activity_date

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/open_date"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="打开日历" />
</LinearLayout>

2、资源文件:

date_sel_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="oval"android:useLevel="false"><solid android:color="@color/colorAccent" /><!--<stroke android:width="1dp" android:color="@color/white"/>--><sizeandroid:width="30dp"android:height="30dp" /></shape>

date_unsel_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="oval"android:useLevel="false"><solid android:color="@color/colorPrimary" /><!--<stroke android:width="1dp" android:color="@color/white"/>--><sizeandroid:width="30dp"android:height="30dp" /></shape>

dialog_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><corners android:radius="5dp" /><solid android:color="#ffffff" />
</shape>

item_date.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:padding="5dp"><TextViewandroid:id="@+id/item_number"android:layout_width="35dp"android:layout_height="35dp"android:gravity="center"android:text="10"android:textSize="15sp" /></LinearLayout>

颜色:

 <color name="colorPrimary">#008577</color><color name="colorPrimaryDark">#00574B</color><color name="colorAccent">#D81B60</color>

样式:

<style name="AlertDialogStyle" parent="@android:style/Theme.Dialog"><item name="android:windowBackground">@android:color/transparent</item><item name="android:windowContentOverlay">@null</item><item name="android:windowIsFloating">true</item><item name="android:windowFrame">@null</item><item name="android:backgroundDimEnabled">true</item><item name="android:windowNoTitle">true</item><item name="android:windowIsTranslucent">true</item></style>

安卓打造自己的日历控件相关推荐

  1. 安卓学习笔记--- Android自定义View(CustomCalendar-定制日历控件)

    最近需要做一个日历的控件,感觉使用系统的不能满足自己需求,发现了一个比较不错的自定义日历控件,博主写的很好,转载支持一下. 转载地址: http://blog.csdn.net/xmxkf/artic ...

  2. android仿小米日历,实现一个仿小米日历控件

    先看效果图: 效果图 根据效果,我们可以看到,要实现该控件,需要具备: 容器以及触摸事件处理 周日历布局以及选择,切换上下周处理 月日历布局以及选择,切换上下月处理 首先说说容器 对于其他使用者来说, ...

  3. Android开源的精美日历控件,热插拔设计的万能自定义UI,flutter调用原生sdk

    XML用法 如果需要在日历控件下方使用其它控件,使用CalendarLayout控件即可,calendar_content_view_id为其它控件的id,支持任意控件,如RecyclerView.L ...

  4. JavaScript blog式日历控件javascript

    javascript blog式日历控件 近来要做一个记事本系统,想找一个合适的日历控件,但网上的都是那种日历选择控件. 于是到qq的记事本系统找了一个,但里面的算法有点落后,所以用了它的样式自己写了 ...

  5. Selenium2+python自动化25-js处理日历控件(修改readonly属性)

    前言 日历控件是web网站上经常会遇到的一个场景,有些输入框是可以直接输入日期的,有些不能,以我们经常抢票的12306网站为例,详细讲解如何解决日历控件为readonly属性的问题. 基本思路:先用j ...

  6. php获取WdatePicker值,WdatePicker日历控件使用方法

    wdatepicker日历控件是个非常好用的js日历控件,这搜集了一下他的使用方法,这个js可以在这里下载:http://l4.yunpan.cn/lk/QEVi5KP4CqgJ2 1. 跨无限级框架 ...

  7. My97DatePicker日历控件日报、每周和每月的选择

    My97DatePicker日历控件日报.每周和每月的选择 1.设计源代码 <%@ page language="java" import="java.util.* ...

  8. WdatePicker 日历控件的onchange事件无作用

    2019独角兽企业重金招聘Python工程师标准>>> 来源:https://blog.csdn.net/gaoxiong335/article/details/70146664 在 ...

  9. layui时间怎么设置年月日时分秒_layui-laydate时间日历控件使用方法详解

    本文实例为大家分享了laydate时间日历控件的使用方法,供大家参考,具体内容如下 此控件可使用layui或者独立版的layDate,两者初始化有些不同 在 layui 模块中使用layui.code ...

  10. Ajax Toolkit日历控件CalendarExtender求教!

    Ajax Toolkit新控件CalendarExtender(日历控件) 在DIV中被下拉框遮挡了,如何能让其不被遮挡? 转载于:https://www.cnblogs.com/jerryleee1 ...

最新文章

  1. 阿里云视频点播获取视频点播的video信息
  2. windows下安装nodejs、webpack及打包步骤
  3. python 集合
  4. 47、Power Query-处理典型的中国式二维表格转一维
  5. C++描述 国王给骑士发放金币 ||
  6. html渐变编织背景,【报Bug】uniapp设置了css渐变背景色编译时会报错
  7. 单机安装oracle,单机安装oracle系统
  8. 大数据之-Hadoop之HDFS_HDFS_基于JAVA的开发_客户端环境准备---大数据之hadoop工作笔记0054
  9. Python中猜数字游戏
  10. mysql强制安装参数_Mysql编译安装参数优化
  11. 转 matlab卷积函数介绍 conv filter conv2
  12. andriod studio 自带模拟器设置开发者模式
  13. maya阿诺德渲染失败_[转载]Arnold 渲染器for maya 安装不了的解决办法
  14. 上海公交投诉电话:12319
  15. WPF真入门教程21--WPF资源系统
  16. 数字图像处理 拜耳过滤器简介
  17. Web安全第 01 讲:渗透测试方法论
  18. 磁盘阵列柜 和存储有什么区别
  19. 22-08-06 西安 尚医通(03)EasyExcel; Spring Cache 、Redis做缓存
  20. 新型高科技口罩即将成为一种身份象征

热门文章

  1. 未能联接game center服务器,Game Center无法毗邻服务器怎么办 五种方法任你选择
  2. ​ SequoiaDB 简介​,巨杉数据库整体介绍
  3. HiWork告诉你:拿什么来拯救你,我的时间!
  4. xposed绕过模拟器检测_《绝地求生》手游避开模拟器检测攻略分享
  5. git - 1.基础
  6. 设置linux定时任务,linux定时任务的设置
  7. 第三方支付平台:微信支付接口
  8. clustream java_数据流聚类算法
  9. 离职时,是在公司群里大方告别,主动退群?还是一言不发,默默退出?
  10. 混乱的代码是技术债吗