欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android自定义日历效果

程序员文章站 2022-04-28 21:01:51
因为工作功能需求,自定义一个日历,效果如下,点击选中日历使用github上面一个前辈的框架implementation 'com.necer.ncalendar:ncalendar:5.0.0'imp...

因为工作功能需求,自定义一个日历,效果如下,点击选中日历

Android自定义日历效果

使用github上面一个前辈的框架

implementation 'com.necer.ncalendar:ncalendar:5.0.0'
implementation 'com.github.codingending:popuplayout:v1.0'//poplayout

框架使用基本类型地址,大家可以根据需要学习修改:地址

自定义日历的xml文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 tools:context=".calendaractivity">

 <view
  android:id="@+id/title_bar"
  android:layout_width="320dp"
  android:layout_height="40dp"
  android:background="@drawable/calendar_bg"
  app:layout_constraintend_toendof="parent"
  app:layout_constraintstart_tostartof="parent"
  app:layout_constrainttop_totopof="parent" />

 <textview
  android:id="@+id/year"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginstart="18dp"
  android:text="2018"
  android:textcolor="#ffffff"
  android:textsize="16sp"
  app:layout_constraintbottom_tobottomof="@id/title_bar"
  app:layout_constraintstart_tostartof="@id/title_bar"
  app:layout_constrainttop_totopof="@id/title_bar" />

 <textview
  android:id="@+id/month"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginstart="18dp"
  android:text="四"
  android:textcolor="#ffffff"
  android:textsize="18sp"
  app:layout_constraintbottom_tobottomof="@id/title_bar"
  app:layout_constraintend_toendof="@id/title_bar"
  app:layout_constraintstart_tostartof="@id/title_bar"
  app:layout_constrainttop_totopof="@id/title_bar" />
 <textview
  android:id="@+id/monthname"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="月"
  android:textcolor="#ffffff"
  android:textsize="18sp"
  app:layout_constraintbottom_tobottomof="@id/title_bar"
  app:layout_constraintstart_toendof="@id/month"
  app:layout_constrainttop_totopof="@id/title_bar" />
 <com.necer.view.weekbar
  android:id="@+id/week"
  android:layout_width="320dp"
  android:layout_height="wrap_content"
  android:layout_margintop="10dp"
  app:layout_constraintend_toendof="parent"
  app:layout_constraintstart_tostartof="parent"
  app:layout_constrainttop_tobottomof="@id/title_bar" />

 <com.necer.calendar.monthcalendar
  android:id="@+id/month_calendar"
  android:layout_width="320dp"
  android:layout_height="280dp"
  app:layout_constraintend_toendof="parent"
  app:layout_constraintstart_tostartof="parent"
  app:layout_constrainttop_tobottomof="@id/week" />

 <view
  android:id="@+id/bottom_view"
  android:layout_width="320dp"
  android:layout_height="40dp"
  android:background="@drawable/calendar_bg_bottom"
  app:layout_constraintend_toendof="parent"
  app:layout_constraintstart_tostartof="parent"
  app:layout_constrainttop_tobottomof="@id/month_calendar" />

 <textview
  android:id="@+id/lastyear"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:onclick="lastyear"
  android:text="上一年"
  android:textcolor="#ffffff"
  app:layout_constraintbottom_tobottomof="@id/bottom_view"
  app:layout_constraintend_tostartof="@id/dividerone"
  app:layout_constraintstart_tostartof="@id/bottom_view"
  app:layout_constrainttop_totopof="@id/bottom_view" />

 <view
  android:id="@+id/dividerone"
  android:layout_width="1dp"
  android:layout_height="40dp"
  android:background="#ffffff"
  app:layout_constraintend_tostartof="@id/lastmonth"
  app:layout_constraintstart_toendof="@id/lastyear"
  app:layout_constrainttop_totopof="@id/bottom_view" />

 <textview
  android:id="@+id/lastmonth"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:onclick="lastmonth"
  android:text="上个月"
  android:textcolor="#ffffff"
  app:layout_constraintbottom_tobottomof="@id/bottom_view"
  app:layout_constraintend_tostartof="@id/dividertwo"
  app:layout_constraintstart_toendof="@id/dividerone"
  app:layout_constrainttop_totopof="@id/bottom_view" />

 <view
  android:id="@+id/dividertwo"
  android:layout_width="1dp"
  android:layout_height="40dp"
  android:background="#ffffff"
  app:layout_constraintend_tostartof="@id/nextmonth"
  app:layout_constraintstart_toendof="@id/lastmonth"
  app:layout_constrainttop_totopof="@id/bottom_view" />

 <textview
  android:id="@+id/nextmonth"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:text="下个月"
  android:textcolor="#ffffff"
  android:onclick="nextmonth"
  app:layout_constraintbottom_tobottomof="@id/bottom_view"
  app:layout_constraintend_tostartof="@id/dividerthree"
  app:layout_constraintstart_toendof="@id/dividertwo"
  app:layout_constrainttop_totopof="@id/bottom_view" />

 <view
  android:id="@+id/dividerthree"
  android:layout_width="1dp"
  android:layout_height="40dp"
  android:background="#ffffff"
  app:layout_constraintend_tostartof="@id/nextyear"
  app:layout_constraintstart_toendof="@id/nextmonth"
  app:layout_constrainttop_totopof="@id/bottom_view" />

 <textview
  android:id="@+id/nextyear"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:text="下一年"
  android:textcolor="#ffffff"
  android:onclick="nextyear"
  app:layout_constraintbottom_tobottomof="@id/bottom_view"
  app:layout_constraintend_toendof="@id/bottom_view"
  app:layout_constraintstart_toendof="@id/dividerthree"
  app:layout_constrainttop_totopof="@id/bottom_view" />
</androidx.constraintlayout.widget.constraintlayout>

mainactivity,日历的功能重写也是在和这个函数中

package com.example.calendartest;

import androidx.appcompat.app.appcompatactivity;

import android.os.bundle;
import android.os.handler;
import android.view.view;
import android.widget.button;
import android.widget.textview;

import com.codingending.popuplayout.popuplayout;
import com.necer.calendar.basecalendar;
import com.necer.calendar.monthcalendar;
import com.necer.enumeration.checkmodel;
import com.necer.enumeration.datechangebehavior;
import com.necer.listener.oncalendarchangedlistener;

import org.joda.time.localdate;

public class mainactivity extends appcompatactivity {
 popuplayout popuplayout;
 view calendarview;
 textview myear, mmonth, lastyear, nextyear, lastmonth, nextmonth;
 monthcalendar monthcalendar;
 int currentyear, currentmonth;
 button intent;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  intent = findviewbyid(r.id.intent);
  calendarview = view.inflate(this, r.layout.calendar, null);
  popuplayout = popuplayout.init(this, calendarview);

 }

 public void intent(view view) {
  initcalendar();
  popuplayout.show(popuplayout.position_center);
 }

 public void initcalendar() {
  monthcalendar = calendarview.findviewbyid(r.id.month_calendar);
  myear = calendarview.findviewbyid(r.id.year);
  mmonth = calendarview.findviewbyid(r.id.month);
  lastyear = calendarview.findviewbyid(r.id.lastyear);
  nextyear = calendarview.findviewbyid(r.id.nextyear);
  lastmonth = calendarview.findviewbyid(r.id.lastmonth);
  nextmonth = calendarview.findviewbyid(r.id.nextmonth);
  monthcalendar.setcheckmode(checkmodel.single_default_unchecked);

  monthcalendar.setoncalendarchangedlistener(new oncalendarchangedlistener() {
   @override
   public void oncalendarchange(basecalendar basecalendar, int year, int month, localdate localdate, datechangebehavior datechangebehavior) {
    myear.settext(string.valueof(year));
    mmonth.settext(string.valueof(month));
    intent.settext(string.valueof(localdate));
    currentyear = year;
    currentmonth = month;
    new handler().postdelayed(new runnable() {
     @override
     public void run() {
      popuplayout.dismiss();
     }
    },800);
   }
  });
 }

 public void lastmonth(view view) {
  monthcalendar.tolastpager();
 }

 public void nextmonth(view view) {
  monthcalendar.tonextpager();
 }

 public void nextyear(view view) {
  monthcalendar.jumpdate(currentyear + 1, currentmonth, 1);
 }

 public void lastyear(view view) {
  monthcalendar.jumpdate(currentyear - 1, currentmonth, 1);
 }
}

github下载地址

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

相关标签: Android 日历