给我一个Android 日历控件代码
时间: 2024-03-18 12:04:26 浏览: 66
好的,下面是一个简单的 Android 日历控件的代码示例:
```
public class CalendarView extends LinearLayout {
private static final String TAG = CalendarView.class.getSimpleName();
private TextView tvMonthYear;
private GridView gvCalendar;
private Calendar currentDate = Calendar.getInstance();
private List<Date> dates = new ArrayList<>();
private SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM yyyy", Locale.getDefault());
private CalendarGridAdapter adapter;
public CalendarView(Context context) {
super(context);
}
public CalendarView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initControl(context, attrs);
}
public CalendarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initControl(context, attrs);
}
private void initControl(Context context, AttributeSet attrs) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.calendar_view, this);
tvMonthYear = findViewById(R.id.tv_month_year);
gvCalendar = findViewById(R.id.gv_calendar);
gvCalendar.setOnItemClickListener((parent, view, position, id) -> {
Date date = (Date) parent.getItemAtPosition(position);
Log.d(TAG, "Selected date: " + date.toString());
});
adapter = new CalendarGridAdapter(context, dates, currentDate);
gvCalendar.setAdapter(adapter);
refreshCalendar();
}
private void refreshCalendar() {
dates.clear();
Calendar monthCalendar = (Calendar) currentDate.clone();
monthCalendar.set(Calendar.DAY_OF_MONTH, 1);
int firstDayOfWeek = monthCalendar.get(Calendar.DAY_OF_WEEK) - 1;
monthCalendar.add(Calendar.DAY_OF_MONTH, -firstDayOfWeek);
while (dates.size() < 42) {
dates.add(monthCalendar.getTime());
monthCalendar.add(Calendar.DAY_OF_MONTH, 1);
}
adapter.notifyDataSetChanged();
tvMonthYear.setText(dateFormat.format(currentDate.getTime()));
}
public void setPreviousMonth() {
currentDate.add(Calendar.MONTH, -1);
refreshCalendar();
}
public void setNextMonth() {
currentDate.add(Calendar.MONTH, 1);
refreshCalendar();
}
private static class CalendarGridAdapter extends ArrayAdapter<Date> {
private final LayoutInflater inflater;
private final Calendar currentCalendar;
private final Calendar todayCalendar;
public CalendarGridAdapter(@NonNull Context context, List<Date> dates, Calendar currentCalendar) {
super(context, R.layout.calendar_day, dates);
this.currentCalendar = currentCalendar;
this.todayCalendar = Calendar.getInstance();
inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Date date = getItem(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.calendar_day, parent, false);
}
TextView tvDay = convertView.findViewById(R.id.tv_day);
tvDay.setText(String.valueOf(date.getDate()));
if (date.getMonth() != currentCalendar.get(Calendar.MONTH)) {
tvDay.setTextColor(Color.GRAY);
} else if (date.equals(todayCalendar.getTime())) {
tvDay.setTextColor(Color.RED);
} else {
tvDay.setTextColor(Color.BLACK);
}
return convertView;
}
}
}
```
这个控件使用了一个 GridView 来显示日历,每个单元格就是一个日期。在构造函数中,我们初始化了一些必要的变量和控件,设置了 GridView 的适配器,以及调用了 refreshCalendar() 来刷新日历。
refreshCalendar() 方法是核心方法,用于计算当前月份的所有日期,并将其添加到 dates 列表中,然后通知适配器进行更新。
其他一些方法,如 setPreviousMonth() 和 setNextMonth(),用于在用户点击前一个月和后一个月按钮时切换到相应的月份。
这只是一个简单的示例,您可以根据自己的需求进行更改和扩展。
阅读全文