本文实例为大家分享了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 selWeekList = new ArrayList<>();
private List 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 s