当前位置:   article > 正文

Android自定义日历控件_android 自定义日历控件

android 自定义日历控件

1、效果图:
在这里插入图片描述

 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@drawable/dialog_shape"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="10dp"
            android:gravity="center"
            android:orientation="horizontal">

            <Button
                android:id="@+id/last_month"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="上个月" />

            <TextView
                android: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" />

            <Button
                android:id="@+id/next_month"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="下个月" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="left"
                android:text="周日" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="周一" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="周二" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="周三" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="周四" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="周五" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="right"
                android:text="周六" />
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_select_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <View
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="#cdcdcd" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <TextView
                android: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="取消" />

            <View
                android:layout_width="0.5dp"
                android:layout_height="match_parent"
                android:background="#cdcdcd" />

            <TextView
                android: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:

  1. public class ScreenUtils {
  2. public static int getScreenWidth(Context context) {
  3. DisplayMetrics localDisplayMetrics = new DisplayMetrics();
  4. ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);
  5. return localDisplayMetrics.widthPixels;
  6. }
  7. }

 DateBean:

  1. public class DateBean {
  2. private int week;
  3. private boolean sign;
  4. //0 上月 1本月 2下月
  5. private int month;
  6. private boolean isFlag;
  7. public boolean isFlag() {
  8. return isFlag;
  9. }
  10. public void setFlag(boolean flag) {
  11. isFlag = flag;
  12. }
  13. public int getWeek() {
  14. return week;
  15. }
  16. public void setWeek(int week) {
  17. this.week = week;
  18. }
  19. public boolean isSign() {
  20. return sign;
  21. }
  22. public void setSign(boolean sign) {
  23. this.sign = sign;
  24. }
  25. public int getMonth() {
  26. return month;
  27. }
  28. public void setMonth(int month) {
  29. this.month = month;
  30. }
  31. public DateBean(int week, boolean sign, int month) {
  32. this.week = week;
  33. this.sign = sign;
  34. this.month = month;
  35. }
  36. }

适配器:DateAdapter

  1. public class DateAdapter extends CommonRecyclerAdapter<DateBean> {
  2. private static final String TAG = "DateAdapter";
  3. private Integer nowDay;
  4. private int year;
  5. private int month;
  6. public DateAdapter(Context context, List<DateBean> list) {
  7. super(context, R.layout.item_date, list);
  8. }
  9. public void setNowDay(int year, int month, int nowDay) {
  10. this.nowDay = nowDay;
  11. this.year = year;
  12. this.month = month;
  13. notifyDataSetChanged();
  14. }
  15. @Override
  16. public void convert(RecyclerHolder holder, DateBean item, int position) {
  17. TextView number = holder.getView(R.id.item_number);
  18. number.setText(item.getWeek() + "");
  19. //当前年月等于切换年月时才选中当天
  20. if (position == nowDay) {
  21. String date = year + "-" + month;
  22. if (date.equals(DataUtils.timeInMillsTrasToDate(1))) {
  23. number.setBackgroundResource(R.drawable.date_unsel_shape);
  24. number.setTextColor(Color.WHITE);
  25. }else {
  26. number.setTextColor(Color.parseColor("#333333"));
  27. }
  28. } else {
  29. number.setBackgroundResource(0);
  30. number.setTextColor(Color.parseColor("#333333"));
  31. if (item.getMonth() == 0 || item.getMonth() == 2) {
  32. number.setTextColor(Color.parseColor("#CDCDCD"));
  33. } else {
  34. number.setTextColor(Color.parseColor("#333333"));
  35. }
  36. }
  37. //点击事件
  38. number.setOnClickListener(view -> {
  39. //本月可以点击
  40. int nowYear = Integer.parseInt(DataUtils.timeInMillsTrasToDate(2));
  41. int nowMonth = Integer.parseInt(DataUtils.timeInMillsTrasToDate(3));
  42. //只有在今天之后的日期才可以选中
  43. if (year == nowYear) {
  44. if (item.getMonth() == 1 && month == nowMonth && position > nowDay) {
  45. onClick(item, number);
  46. } else if (month > nowMonth) {
  47. onClick(item, number);
  48. }
  49. } else if (year > nowYear) {
  50. onClick(item, number);
  51. }
  52. });
  53. }
  54. private void onClick(DateBean item, TextView number) {
  55. if (item.isFlag()) {
  56. item.setFlag(false);
  57. number.setBackgroundResource(0);
  58. number.setTextColor(Color.parseColor("#333333"));
  59. } else {
  60. item.setFlag(true);
  61. number.setBackgroundResource(R.drawable.date_sel_shape);
  62. number.setTextColor(Color.WHITE);
  63. }
  64. }
  65. }

数据源:DataUtils

  1. public class DataUtils {
  2. private static final String TAG = "DataUtils";
  3. /**
  4. * 日历
  5. */
  6. public static List<DateBean> getCalendar(int currentYear, int currentMonth) {
  7. //实例化集合
  8. List<DateBean> list = new ArrayList<>();
  9. Calendar c = Calendar.getInstance();
  10. c.clear();
  11. /**
  12. * 处理上个月月末
  13. */
  14. if (currentMonth - 1 == 0) {
  15. c.set(Calendar.YEAR, currentYear - 1);
  16. c.set(Calendar.MONTH, 11);
  17. } else {
  18. c.set(Calendar.YEAR, currentYear);
  19. c.set(Calendar.MONTH, (currentMonth - 2));
  20. }
  21. int last_sumDays = c.getActualMaximum(Calendar.DAY_OF_MONTH);
  22. c.set(Calendar.DATE, last_sumDays);
  23. //得到一号星期几
  24. int weekDay = c.get(Calendar.DAY_OF_WEEK);
  25. if (weekDay < 7) {
  26. for (int i = weekDay - 1; i >= 0; i--) {
  27. Integer date = new Integer(last_sumDays - i);
  28. list.add(new DateBean(date, true, 0));
  29. }
  30. }
  31. /**
  32. * 处理当前月
  33. */
  34. c.clear();
  35. c.set(Calendar.YEAR, currentYear);
  36. c.set(Calendar.MONTH, currentMonth - 1);
  37. int currentsumDays = c.getActualMaximum(Calendar.DAY_OF_MONTH);
  38. for (int i = 1; i <= currentsumDays; i++) {
  39. Integer date = new Integer(i);
  40. list.add(new DateBean(date, true, 1));
  41. }
  42. /**
  43. * 处理下个月月初
  44. */
  45. c.clear();
  46. if (currentMonth == 12) {
  47. c.set(Calendar.YEAR, currentYear + 1);
  48. c.set(Calendar.MONTH, 0);
  49. } else {
  50. c.set(Calendar.YEAR, currentYear);
  51. c.set(Calendar.MONTH, currentMonth);
  52. }
  53. c.set(Calendar.DATE, 1);
  54. int currentWeekDay = c.get(Calendar.DAY_OF_WEEK);
  55. if (currentWeekDay > 2 && currentWeekDay < 8) {
  56. for (int i = 1; i <= 8 - currentWeekDay; i++) {
  57. list.add(new DateBean(i, true, 2));
  58. }
  59. }
  60. return list;
  61. }
  62. /**
  63. * 获取上个月大小
  64. */
  65. public static Integer getLastMonth(int currentYear, int currentMonth) {
  66. //实例化集合
  67. List<Integer> list = new ArrayList<>();
  68. Calendar c = Calendar.getInstance();
  69. c.clear();
  70. /**
  71. * 处理上个月月末
  72. */
  73. if (currentMonth - 1 == 0) {
  74. c.set(Calendar.YEAR, currentYear - 1);
  75. c.set(Calendar.MONTH, 11);
  76. } else {
  77. c.set(Calendar.YEAR, currentYear);
  78. c.set(Calendar.MONTH, (currentMonth - 2));
  79. }
  80. int last_sumDays = c.getActualMaximum(Calendar.DAY_OF_MONTH);
  81. c.set(Calendar.DATE, last_sumDays);
  82. //得到一号星期几
  83. int weekDay = c.get(Calendar.DAY_OF_WEEK);
  84. if (weekDay < 7) {
  85. for (int i = weekDay - 1; i >= 0; i--) {
  86. list.add(i);
  87. }
  88. }
  89. Log.e(TAG, "getLastMonth: " + Integer.parseInt(timeInMillsTrasToDate(0)));
  90. return list.size() + Integer.parseInt(timeInMillsTrasToDate(0)) - 1;
  91. }
  92. /**
  93. * list转string字符串
  94. */
  95. public static String returnList(List<String> list) {
  96. if (null == list && list.size() == 0) {
  97. return "";
  98. } else {
  99. //去除空格
  100. String str = String.valueOf(list).replaceAll(" ", "");
  101. return str.substring(1, str.length() - 1);
  102. }
  103. }
  104. /**
  105. * 时间转换
  106. */
  107. @TargetApi(Build.VERSION_CODES.N)
  108. public static String timeInMillsTrasToDate(int formatType) {
  109. DateFormat formatter = null;
  110. switch (formatType) {
  111. case 0:
  112. formatter = new SimpleDateFormat("dd");
  113. break;
  114. case 1:
  115. formatter = new SimpleDateFormat("yyyy-MM");
  116. break;
  117. case 2:
  118. formatter = new SimpleDateFormat("yyyy");
  119. break;
  120. case 3:
  121. formatter = new SimpleDateFormat("MM");
  122. break;
  123. }
  124. Calendar calendar = Calendar.getInstance();
  125. return formatter.format(calendar.getTime());
  126. }
  127. }

使用:DateActivity

  1. public class DateActivity extends AppCompatActivity {
  2. private Button openDate;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_date);
  7. openDate = this.findViewById(R.id.open_date);
  8. openDate.setOnClickListener(view -> {
  9. showDateDialog();
  10. });
  11. }
  12. private void showDateDialog() {
  13. int year = Integer.parseInt(DataUtils.timeInMillsTrasToDate(2));
  14. int month = Integer.parseInt(DataUtils.timeInMillsTrasToDate(3));
  15. new SelectDateDialog().builder(this, year, month)
  16. .setListener(selectDate -> openDate.setText(selectDate)).show();
  17. }
  18. }

对应布局: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">

    <Button
        android: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"/>-->
    <size
        android: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"/>-->
    <size
        android: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">

    <TextView
        android:id="@+id/item_number"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:gravity="center"
        android:text="10"
        android:textSize="15sp" />

</LinearLayout>

 样式:

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/930110
推荐阅读
相关标签
  

闽ICP备14008679号