本文实例为大家分享了Android日历控件的实现代码,供大家参考,具体内容如下
1、效果图:
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); } }