做的是一个酒店的项目,可以选择入住和离开的日期。声明为了省事在网上找的资料,自己修改的逻辑,希望对需要的朋友有帮助。喜欢的给个好评。谢谢啦!祝生活愉快!
先上图
第一步,搭建布局xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/days_detail"
android:gravity="center"
android:padding="16dp"
android:text="选择住店离店日期"
android:textColor="@color/white"
android:textSize="18sp" />
<ImageView
android:id="@+id/back_to_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:src="@drawable/backto" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@color/days_detail" />
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="20dp"
android:padding="10dp"
app:cardCornerRadius="10dp"
app:cardElevation="16dp">
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</android.support.v7.widget.CardView>
</FrameLayout>
</LinearLayout>
</ScrollView>
第二部在编写逻辑
LinearLayout ll;
MyCalendar c1;
Date date;
String nowday;
long nd = 1000 * 24L * 60L * 60L;//一天的毫秒数
SimpleDateFormat simpleDateFormat, sd1, sd2;
SharedPreferences sp;
SharedPreferences.Editor editor;
private String inday, outday//日期
sp_inday, sp_outday;//周几
Activity extends BaseActivity implements MyCalendar.OnDaySelectListener {
继承BaseActivity实现点击日历的监听回调
private void init() {
List<String> listDate = getDateList();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < listDate.size(); i++) {
c1 = new MyCalendar(this);
c1.setLayoutParams(params);
Date date = null;
try {
date = simpleDateFormat.parse(listDate.get(i));
} catch (ParseException e) {
e.printStackTrace();
}
if (!"".equals(sp_inday)) {
c1.setInDay(sp_inday);
}
if (!"".equals(sp_outday)) {
c1.setOutDay(sp_outday);
}
c1.setTheDay(date);
c1.setOnDaySelectListener(this);
ll.addView(c1);
}
}
@Override
public void onDaySelectListener(View view, String date) {
//若日历日期小于当前日期,或日历日期-当前日期超过三个月,则不能点击
try {
if (simpleDateFormat.parse(date).getTime() < simpleDateFormat.parse(nowday).getTime()) {
return;
}
long dayxc = (simpleDateFormat.parse(date).getTime() - simpleDateFormat.parse(nowday).getTime()) / nd;
if (dayxc > 90) {
return;
}
} catch (ParseException e) {
e.printStackTrace();
}
//若以前已经选择了日期,则在进入日历后会显示以选择的日期,该部分作用则是重新点击日历时,清空以前选择的数据(包括背景图案)
if (!"".equals(sp_inday)) {
c1.viewIn.setBackgroundColor(Color.WHITE);
((TextView) c1