插件效果如图所示:
使用:
引入:在build.gradle中添加 implementation'com.prolificinteractive:material-calendarview:1.4.0';
应用:
(1)所在的activity要
implements CalFragment.OnFrgDataListener
(2)
CalFragment calendarViewFragment= new CalFragment();calendarViewFragment.show(getSupportFragmentManager(), "timeChoose");
(3)重写CalFragment
public class CalFragment extends DialogFragment implements OnDateSelectedListener {DialogFragment mContext = this;int flag = 0;private View view;MaterialCalendarView mcv;//接收从Activity传来的数据Bundle timeBundle;OnFrgDataListener IListener;public CalFragment() {// Required empty public constructor}@SuppressLint("ValidFragment")public CalFragment(int flag) {this.flag = flag;}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {view = inflater.inflate(R.layout.fragment_cal, container, false);mcv = view.findViewById(R.id.mcv);timeBundle = getArguments();initData();return view;}@Overridepublic void onStart() {super.onStart();//下面这些都是为了让dialog宽度全充满Window win = getDialog().getWindow();// 一定要设置Background,如果不设置,window属性设置无效win.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.white)));DisplayMetrics dm = new DisplayMetrics();getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);WindowManager.LayoutParams params = win.getAttributes();// 使用ViewGroup.LayoutParams,以便Dialog 宽度充满整个屏幕params.width = ViewGroup.LayoutParams.MATCH_PARENT;params.height = ViewGroup.LayoutParams.WRAP_CONTENT;win.setAttributes(params);/*if (dialog != null) {DisplayMetrics dm = new DisplayMetrics();getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);dialog.getWindow().setLayout((int) (dm.widthPixels * 1), ViewGroup.LayoutParams.WRAP_CONTENT);}*/}@Overridepublic void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);//这个主题 背景全透明 没有半透明// setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Holo_Light);}public void initData() {// 显示兴起补全的整个礼拜的上个月或者下个月的日期 一般会多出一行整个礼拜// 点击补全出来的另外一个月的信息 可以直接跳到那个月mcv.setShowOtherDates(MaterialCalendarView.SHOW_ALL);// 设置日历默认的时间为当前的时间mcv.setSelectedDate(new Date());if(flag == 1 ){Calendar c = Calendar.getInstance();/*取得系统日期:*/int year = c.get(Calendar.YEAR);int month = c.get(Calendar.MONTH);int day = c.get(Calendar.DAY_OF_MONTH);// 日历的主要设置mcv.state().edit()// 设置你的日历 第一天是周一还是周一.setFirstDayOfWeek(Calendar.MONDAY)// 设置你的日历的最小的月份 月份为你设置的最小月份的下个月 比如 你设置最小为1月 其实你只能滑到2月/*.setMinimumDate(CalendarDay.from(, 1, 1))*/// 同最小 设置最大.setMaximumDate(CalendarDay.from(year, month, day))// 设置你的日历的模式 是按月 还是按周.setCalendarDisplayMode(CalendarMode.MONTHS).commit();} else {// 日历的主要设置mcv.state().edit()// 设置你的日历 第一天是周一还是周一.setFirstDayOfWeek(Calendar.MONDAY)// 设置你的日历的最小的月份 月份为你设置的最小月份的下个月 比如 你设置最小为1月 其实你只能滑到2月/*.setMinimumDate(CalendarDay.from(, 1, 1))*/// 设置你的日历的模式 是按月 还是按周.setCalendarDisplayMode(CalendarMode.MONTHS).commit();}// 设置你选中日期的背景底色mcv.setSelectionColor(getResources().getColor(R.color.blueBackOfBtn));mcv.setOnDateChangedListener(this);}/*** 日期选择 回调函数** @param widget* @param date* @param selected*/@Overridepublic void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {Toast.makeText(getActivity(), getSelectedDatesString(), Toast.LENGTH_SHORT).show();// Toast.makeText(getActivity(),"从Activity传递的数据:"+(String)timeBundle.get("date"),Toast.LENGTH_SHORT).show();IListener.progress(mcv.getSelectedDate().getDate());//选择后,延迟消失new Handler().postDelayed(new Runnable() {@Overridepublic void run() {mContext.dismiss();}}, 1000);}/*** 将日期转换为字符串** @return*/private String getSelectedDatesString() {CalendarDay date = mcv.getSelectedDate();if (date == null) {return "No Selection";}Log.i("sinstar", "getSelectedDatesString: " + date.toString());return FORMATTER.format(date.getDate());}private static final DateFormat FORMATTER = SimpleDateFormat.getDateInstance();/*** 定义接口,向Activity传递数据*/public interface OnFrgDataListener {public void progress(Date date);}/*** 注入实例方法** @param*//* public void setOnFrgDataListener(OnFrgDataListener listener){IListener=listener;}*/@Overridepublic void onAttach(Activity activity) {super.onAttach(activity);if (activity instanceof OnFrgDataListener) {IListener = (OnFrgDataListener) activity;} else {throw new IllegalArgumentException("activity must implements OnFrgDataListener");}}@Overridepublic void onDetach() {super.onDetach();IListener = null;}}
(4)设置日历的最大时间
// 同最小 设置最大.setMaximumDate(CalendarDay.from(year, month, day))
(5)date即为所选时间
@Overridepublic void progress(Date date) {//每次选择日期后,都会调用这个方法if (date != null) {str = ParseDate.dateParseString(date);query_1.setText(str);}}