Android自定义日历效果

这篇文章主要为大家详细介绍了Android自定义日历效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

 implementation 'com.necer.ncalendar:ncalendar:5.0.0' implementation 'com.github.CodingEnding:PopupLayout:v1.0'//poplayout

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

自定义日历的xml文件

  

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自定义日历效果的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动