xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_white"
android:orientation="vertical"
android:paddingHorizontal="@dimen/dimens_16dp"
android:paddingTop="@dimen/dimens_16dp">
<CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_confirm"
android:layout_width="match_parent"
android:layout_height="@dimen/dimens_44dp"
android:layout_marginHorizontal="@dimen/dimens_16dp"
android:background="@drawable/bg_radius8_0f41a6"
android:gravity="center"
android:text="@string/limit_time_define"
android:textColor="#ffffffff"
android:textSize="16sp"
android:layout_marginTop="@dimen/dimens_10dp"
android:layout_marginBottom="@dimen/dimens_10dp"
/>
</LinearLayout>
java代码:
//声明
private long calendarTime;
private Calendar calendar;
//使用
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
//显示用户选择的日期
Util.showToast(mContext, year + "年" + (month + 1) + "月" + dayOfMonth + "日");
if (calendar == null) {
calendar = Calendar.getInstance();
}
calendar.set(year, month, dayOfMonth);
calendarTime = calendar.getTimeInMillis();
}
});
//点击事件
public void onViewClicked() {
long systemTime = System.currentTimeMillis();
if (systemTime > calendarTime && DateUtil.compareToday(calendarTime) != 0) {
Util.showToast(mContext, R.string.unchose_time);
} else {
Util.showToast(mContext, R.string.current_time);
}
LogUtils.e("time", "calendarTime: " + calendarTime + "systemTime" + systemTime);
}