最后我决定我需要在下方显示一个“确定”按钮,CalendarView
以便可以选择日期并取消日历。
因为当您单击当前日期时,没有任何反应,而当您滚动时,您会接到OnDateChangeListener
.
带有黑色蒙版背景的日历布局示例,您可以单击以关闭:
<LinearLayout
android:id="@+id/calendar_layout"
android:onClick="dismissCalendar"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:background="@color/black_mask">
<CalendarView
android:id="@+id/calendar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white"
android:showWeekNumber="false"
android:firstDayOfWeek="2"
/>
<TextView
android:onClick="selectDate"
android:text="@string/select_date"
android:background="@color/colorPrimary"
style="@style/settings_button"/>
</LinearLayout>
然后示例代码:
private void openCalendar(Date date) {
calendar.setDate(date.getTime());
calendarLayout.setVisibility(View.VISIBLE);
}
public void selectDate(View view) {
Date date = new Date(calendar.getDate());
// do whatever you need with date
dismissCalendar(view);
}
public void dismissCalendar(View view) {
calendarLayout.setVisibility(View.INVISIBLE);
}