Android自定义日历效果

所属分类: 软件编程 / Android 阅读数: 99
收藏 0 赞 0 分享

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

使用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网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多