服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android 自定义日期段选择控件功能(开始时间-结束时间)

Android 自定义日期段选择控件功能(开始时间-结束时间)

2022-12-12 14:31mxy19891106 Android

这篇文章主要介绍了Android 自定义日期段选择控件功能,开始时间-结束时间。本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

开发中碰到个需求,需要在一个空间中选择完成开始和结束时间。实现的过程走的是程序员开发的老路子,找到轮子后自己改吧改吧就成了。

当时做的时候有几个需求:1.当天为最大的结束日期,2.最大选择范围1年,3.开始时间和结束时间可以为同一天。如有其他需求实现,可以参考代码改进一下。先上效果图:

Android 自定义日期段选择控件功能(开始时间-结束时间)

视频点击后的虚影是屏幕录制的原因。实现步骤:(如有缺失什么资源,请告知。开始时间和结束时间显示自己布局内添加就可以)

1.自定义控件属性

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<declare-styleable name="MyCalendar">
 <attr name="dateformat" format="string"></attr>
 <attr name="titleSize" format="dimension"></attr>
 <attr name="titleColor" format="color"></attr>
 <attr name="goIcon" format="reference"></attr>
 <attr name="preIcon" format="reference"></attr>
 <attr name="dayInMonthColor" format="color"></attr>
 <attr name="dayOutMonthcolor" format="color"></attr>
 <attr name="todayColor" format="color"></attr>
 <attr name="todayEmptycircleColor" format="color"></attr>
 <attr name="todayFillcircleColor" format="color"></attr>
 <attr name="calendarbackground" format="color|reference"></attr>
</declare-styleable>

2.自定义控件代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/**
 * @Description: 可以选择时间范围的日历控件
 * @Author MengXY
 * @Emil mxy_2012_1@163.com
 * @Date 2019/1/8
*/
public class CalendarView extends LinearLayout implements View.OnClickListener{
 private TextView title;
 private RecyclerView recyclerView;
 private RelativeLayout layout_calendar_gonext;
 private RelativeLayout layout_calendar_goup;
 private LinearLayoutManager linearLayoutManager;
 private Calendar curDate = Calendar.getInstance();
 //从服务器获取的日期
 private Date dateFromServer;
 //外层主recyclerview的adapter
 private MainRvAdapter mainAdapter;
 private List<CalendarCell> months = new ArrayList<>();
 private Context context;
 //相关属性
 private int titleColor;
 private int titleSize;
 private int enableSelectColor;
 private int disableSeletColor;
 private int todayColor;
 private int todayEmptyColor;
 private int todayFillColor;
 /** 初始日期为当前日期前一年*/
 private String time;
 private long timeBefor;
 private long timeNow;
 private List<String> titles = new ArrayList<>();
 //点击的开始时间与结束时间
 private Date sDateTime;
 private Date eDateTime;
 private boolean isSelectingSTime = true;
 private HashMap<Integer, SubRvAdapter> allAdapters = new HashMap<>();
 public CalendarView(Context context) {
 this(context, null);
 }
 public CalendarView(Context context, AttributeSet attrs) {
 this(context, attrs, 0);
 }
 private int maxSelect = 13;
 public CalendarView(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyCalendar);
 titleColor = ta.getColor(R.styleable.MyCalendar_titleColor, Color.WHITE);
 titleSize = (int) ta.getDimension(R.styleable.MyCalendar_titleSize, 15);
 enableSelectColor = ta.getColor(R.styleable.MyCalendar_dayInMonthColor, context.getResources().getColor(R.color.text_lable));
 disableSeletColor = ta.getColor(R.styleable.MyCalendar_dayOutMonthcolor, context.getResources().getColor(R.color.text_unenable));
 todayColor = ta.getColor(R.styleable.MyCalendar_todayColor, Color.BLUE);
 todayEmptyColor = ta.getColor(R.styleable.MyCalendar_todayEmptycircleColor, Color.CYAN);
 todayFillColor = ta.getColor(R.styleable.MyCalendar_todayFillcircleColor, Color.CYAN);
 ta.recycle();
 this.context = context;
 init(context);
 }
 //该方法用于设置从服务器获取的时间,如果没有从服务器获取的时间将使用手机本地时间
 private void initTime() {
 Calendar calendar = Calendar.getInstance(); //得到日历
 calendar.setTime(new Date());
 calendar.add(Calendar.MONTH,-(maxSelect-1));
 time = DateUtils.formatData(calendar.getTime(),Constant.TFORMATE_YMD);
 timeBefor = DateUtils.getDataTime(time);
 String now = DateUtils.formatData(new Date(),Constant.TFORMATE_YMD);
 timeNow = DateUtils.getDataTime(now);
// LogUtils.e("之前日期:"+time+"=="+timeBefor);
// LogUtils.e("当前日期:"+now+"=="+timeNow);
 curDate = DateUtil.strToCalendar(time, Constant.TFORMATE_YMD);
 dateFromServer = DateUtil.strToDate(time, Constant.TFORMATE_YMD);
 }
 private void init(Context context) {
 bindView(context);
 renderCalendar();
 }
 private void bindView(Context context) {
 View view = LayoutInflater.from(context).inflate(R.layout.appoint_calendarview, this, false);
 title = (TextView) view.findViewById(R.id.calendar_title);
 title.setTextColor(titleColor);
 title.setTextSize(titleSize);
 layout_calendar_gonext = view.findViewById(R.id.layout_calendar_gonext);
 layout_calendar_goup = view.findViewById(R.id.layout_calendar_goup);
 layout_calendar_gonext.setOnClickListener(this);
 layout_calendar_goup.setOnClickListener(this);
 recyclerView = (RecyclerView) view.findViewById(R.id.calendar_rv);
 linearLayoutManager = new LinearLayoutManager(this.context, LinearLayoutManager.HORIZONTAL, false);
 recyclerView.setLayoutManager(linearLayoutManager);
 PagerSnapHelper snapHelper = new PagerSnapHelper();
 snapHelper.attachToRecyclerView(recyclerView);
 addView(view);
 }
 public void renderCalendar() {
 months.clear();
 initTime();
 for (int i = 0; i < maxSelect; i++) {
 ArrayList<Date> cells = new ArrayList<>();
 if (i != 0) {
 curDate.add(Calendar.MONTH, 1);//后推一个月
 } else {
 curDate.add(Calendar.MONTH, 0);//当前月
 }
 Calendar calendar = (Calendar) curDate.clone();
 //将日历设置到当月第一天
 calendar.set(Calendar.DAY_OF_MONTH, 1);
 //获得当月第一天是星期几,如果是星期一则返回1此时1-1=0证明上个月没有多余天数
 int prevDays = calendar.get(Calendar.DAY_OF_WEEK) - 1;
 //将calendar在1号的基础上向前推prevdays天。
 calendar.add(Calendar.DAY_OF_MONTH, -prevDays);
 //最大行数是6*7也就是,1号正好是星期六时的情况
 int maxCellcount = 6 * 7;
 while (cells.size() < maxCellcount) {
 cells.add(calendar.getTime());
 //日期后移一天
 calendar.add(calendar.DAY_OF_MONTH, 1);
 }
 months.add(new CalendarCell(i, cells));
 }
 for (int i = 0; i < months.size(); i++) {
 //title格式 2018年6月3日
 String id="codetool">

3.自定义view用到的布局 appoint_calendarview.xml,对应日历控件如下面图片的部分。

Android 自定义日期段选择控件功能(开始时间-结束时间)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?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="match_parent"
 android:layout_marginTop="15dp"
 android:orientation="vertical">
 
 <RelativeLayout
 android:layout_width="match_parent"
 android:layout_height="20dp"
 android:gravity="center_vertical"
 android:orientation="horizontal">
 <TextView
 android:id="@+id/calendar_title"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerInParent="true"
 android:text="2018年"
 android:textColor="@color/text_lable"
 android:textSize="15dp"/>
 <RelativeLayout
 android:id="@+id/layout_calendar_gonext"
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:layout_alignParentRight="true"
 android:paddingLeft="15dp"
 android:paddingRight="15dp"
 >
 <ImageView
 android:layout_width="10dp"
 android:layout_height="10dp"
 android:layout_centerVertical="true"
 android:src="@mipmap/icon_arrow_right" />
 </RelativeLayout>
 <RelativeLayout
 android:id="@+id/layout_calendar_goup"
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:paddingLeft="15dp"
 android:paddingRight="15dp"
 >
 <ImageView
 android:layout_width="10dp"
 android:layout_height="10dp"
 android:layout_centerVertical="true"
 android:src="@mipmap/icon_back_black" />
 </RelativeLayout>
 
 </RelativeLayout>
 
 <LinearLayout
 android:id="@+id/calendar_week_header"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="15dp"
 android:gravity="center_vertical"
 android:orientation="horizontal"
 >
 <TextView
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_gravity="center_vertical"
 android:layout_weight="1"
 android:text="@string/sun"
 android:textAlignment="center"
 android:textColor="#555"
 android:textSize="13dp" />
 
 <TextView
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_gravity="center_vertical"
 android:layout_weight="1"
 android:text="@string/mon"
 android:textAlignment="center"
 android:textColor="#555"
 android:textSize="13dp" />
 
 <TextView
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_gravity="center_vertical"
 android:layout_weight="1"
 android:text="@string/tue"
 android:textAlignment="center"
 android:textColor="#555"
 android:textSize="13dp" />
 
 <TextView
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_gravity="center_vertical"
 android:layout_weight="1"
 android:text="@string/wed"
 android:textAlignment="center"
 android:textColor="#555"
 android:textSize="13dp" />
 
 <TextView
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_gravity="center_vertical"
 android:layout_weight="1"
 android:text="@string/thu"
 android:textAlignment="center"
 android:textColor="#555"
 android:textSize="13dp" />
 
 <TextView
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_gravity="center_vertical"
 android:layout_weight="1"
 android:text="@string/fri"
 android:textAlignment="center"
 android:textColor="#555"
 android:textSize="13dp" />
 
 <TextView
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_gravity="center_vertical"
 android:layout_weight="1"
 android:text="@string/sat"
 android:textAlignment="center"
 android:textColor="#555"
 android:textSize="13dp" />
 </LinearLayout>
 
 <android.support.v7.widget.RecyclerView
 android:id="@+id/calendar_rv"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="10dp"
 android:overScrollMode="never"
 />
</LinearLayout>

定义控件选择后的背景部分:CalendarDayRelativeLayout.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
 
 
public class CalendarDayRelativeLayout extends RelativeLayout {
 public CalendarDayRelativeLayout(Context context) {
 this(context, null);
 }
 
 public CalendarDayRelativeLayout(Context context, AttributeSet attrs) {
 super(context, attrs);
 }
 
 @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
 public void isDurationSat(boolean isSaturday) {
 this.setBackground(getResources().getDrawable(R.drawable.appoint_calendar_sat_bg));
 }
 
 @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
 public void isDurationSun(boolean isSunday) {
 this.setBackground(getResources().getDrawable(R.drawable.appoint_calendar_sun_bg));
 }
 @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
 public void isETime(boolean etime) {
// this.setBackgroundResource(getResources().getDrawable(R.drawable.));
 
 this.setBackground(getResources().getDrawable(R.drawable.appoint_calendar_sat_bg));
 }
 @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
 public void isSTime(boolean stime) {
// this.setBackground(getResources().getDrawable(R.mipmap.appoint_calendar_start_bg));
 this.setBackground(getResources().getDrawable(R.drawable.appoint_calendar_sun_bg));
 }
 
 /**
 * 同一天
 * */
 @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
 public void isSameDay(){
 this.setBackground(getResources().getDrawable(R.drawable.appoint_calendar_same_bg));
 }
}

自定义控件内日期的CalendarDayTextView.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
 
/**
 * @Description: 日历内日期
 * @Author MengXY
 * @Date 2019/1/8
*/
public class CalendarDayTextView extends android.support.v7.widget.AppCompatTextView {
 public boolean isToday;
 private boolean isSTime;
 private boolean isETime;
 private Context context;
 public void setEmptyColor(int emptyColor) {
 this.emptyColor = emptyColor;
 }
 public void setFillColor(int fillColor) {
 this.fillColor = fillColor;
 }
 private int emptyColor = Color.parseColor("#00ff00");
 private int fillColor = Color.parseColor("#00ff00");
 private Paint mPaintSTime;
 private Paint mPaintETime;
 
 public CalendarDayTextView(Context context) {
 super(context);
 initview(context);
 }
 public CalendarDayTextView(Context context, AttributeSet attrs) {
 super(context, attrs);
 initview(context);
 }
 private void initview(Context context) {
 this.context=context;
// mPaintSTime = new Paint(Paint.ANTI_ALIAS_FLAG);
// mPaintSTime.setStyle(Paint.Style.FILL);
// mPaintSTime.setColor(context.getResources().getColor(R.color.date_time_bg));
// mPaintSTime.setStrokeWidth(2);
//
// mPaintETime = new Paint(Paint.ANTI_ALIAS_FLAG);
// mPaintETime.setStyle(Paint.Style.FILL);
// mPaintETime.setColor(context.getResources().getColor(R.color.date_time_bg));
// mPaintETime.setStrokeWidth(2);
 }
 @Override
 protected void onDraw(Canvas canvas) {
 //根据当前逻辑开始时间必须先绘制结束时间
// if (isETime) {
// canvas.save();
// //移动到当前控件的中心,以中心为圆点绘制实心圆
// canvas.translate(getWidth() / 2, getHeight() / 2);
// canvas.drawCircle(0, 0, getWidth() / 2 , mPaintETime);
// canvas.restore();
// //此处必须将圆移动回开始位置,否则文本显示会受到影响
// canvas.translate(0, 0);
// }
//
// if (isSTime) {
// canvas.save();
// //移动到当前控件的中心,以中心为圆点绘制实心圆
// canvas.translate(getWidth() / 2, getHeight() / 2);
// canvas.drawCircle(0, 0, getWidth() / 2 , mPaintSTime);
// canvas.restore();
// //此处必须将圆移动回开始位置,否则文本显示会受到影响
// canvas.translate(0, 0);
// }
 super.onDraw(canvas);
 }
 public void setToday(boolean today) {
 isToday = today;
 this.setTextColor(context.getResources().getColor(R.color.text_main_tab_select));
 }
 public void isETime(boolean etime) {
 isETime = etime;
// this.setTextColor(context.getResources().getColor(R.color.date_time_tv));
// this.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
 isSelected(true);
 }
 public void isSTime(boolean stime) {
 isSTime = stime;
 isSelected(true);
// this.setTextColor(context.getResources().getColor(R.color.date_time_tv));
// this.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
 }
 
 public void isSelected(boolean isSelcted){
 if(isSelcted){
 this.setTextColor(context.getResources().getColor(R.color.date_time_tv));
 this.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
 }else {
 this.setTextColor(context.getResources().getColor(R.color.text_lable));
 }
 }
}

appoint_calendarview.xml

?
1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/appoint_calendarview_item_rv"
 android:layout_width="match_parent"
 android:layout_height="wrap_content">
 
</android.support.v7.widget.RecyclerView>

calendar_text_day.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<com.包名.CalendarDayRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="44dp"
 android:gravity="center"
 android:id="@+id/calendar_day_rl"
 android:layout_marginTop="5dp"
 android:layout_marginBottom="5dp"
 >
 <com..包名.CalendarDayTextView
 android:id="@+id/calendar_day_tv"
 android:layout_width="44dp"
 android:layout_height="44dp"
 android:layout_centerInParent="true"
 android:gravity="center"
 android:textColor="@color/white"
 android:text="31"
 android:includeFontPadding="false"
 android:textSize="13dp"/>
</com..包名.CalendarDayRelativeLayout>

DateUtil.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class DateUtil {
 //Calendar 转化 String
 public static String calendarToStr(Calendar calendar,String format) {
// Calendar calendat = Calendar.getInstance();
 SimpleDateFormat sdf = new SimpleDateFormat(format);
 return sdf.format(calendar.getTime());
 }
 
 //String 转化Calendar
 public static Calendar strToCalendar(String str,String format) {
// String str = "2012-5-27";
 SimpleDateFormat sdf = new SimpleDateFormat(format);
 Date date = null;
 Calendar calendar = null;
 try {
 date = sdf.parse(str);
 calendar = Calendar.getInstance();
 calendar.setTime(date);
 } catch (ParseException e) {
 e.printStackTrace();
 }
 return calendar;
 }
 
 // Date 转化String
 public static String dateTostr(Date date,String format) {
 SimpleDateFormat sdf = new SimpleDateFormat(format);
// String dateStr = sdf.format(new Date());
 String dateStr = sdf.format(date);
 return dateStr;
 }
 
 // String 转化Date
 public static Date strToDate(String str,String format) {
 SimpleDateFormat sdf = new SimpleDateFormat(format);
 Date date = null;
 try {
 date = sdf.parse(str);
 } catch (ParseException e) {
 e.printStackTrace();
 }
 return date;
 }
 
 //Date 转化Calendar
 public static Calendar dateToCalendar(Date date) {
 Calendar calendar = Calendar.getInstance();
 calendar.setTime(date);
 return calendar;
 }
 
 //Calendar转化Date
 public static Date calendarToDate(Calendar calendar) {
 return calendar.getTime();
 }
 
 // String 转成 Timestamp
 public static Timestamp strToTimeStamp(String str) {
// Timestamp ts = Timestamp.valueOf("2012-1-14 08:11:00");
 return Timestamp.valueOf(str);
 }
 
 //Date 转 TimeStamp
 public static Timestamp dateToTimeStamp(Date date,String format) {
 SimpleDateFormat df = new SimpleDateFormat(format);
 String time = df.format(new Date());
 Timestamp ts = Timestamp.valueOf(time);
 return ts;
 }
}

4.资源文件 /drawableappoint_calendar_sat_bg.xml //开始时间

?
1
2
3
4
5
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 <corners android:topRightRadius="44dp" android:bottomRightRadius="44dp"/>
 <size android:height="44dp"/>
 <solid android:color="#41D2C4"/>
</shape>

appoint_calendar_sun_bg.xml //结束时间

?
1
2
3
4
5
6
7
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 <corners
 android:bottomLeftRadius="44dp"
 android:topLeftRadius="44dp" />
 <size android:height="44dp" />
 <solid android:color="#41D2C4" />
</shape>

appoint_calendar_same_bg.xml //开始时间和结束时间是同一天

?
1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
 <solid android:color="@color/date_duration_bg" />
 <corners android:radius="60dp" />
</shape>

/value 

?
1
2
3
4
5
6
7
8
9
<string name="sun">日</string>
<string name="mon">一</string>
<string name="tue">二</string>
<string name="wed">三</string>
<string name="thu">四</string>
<string name="fri">五</string>
<string name="sat">六</string>
 
<color name="date_duration_bg">#41D2C4</color>

5.在activity中使用 activity_selectdate.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?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="match_parent"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:background="@color/white"
 android:orientation="vertical">
 
 <RelativeLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="40dp">
 
 <FrameLayout
 android:id="@+id/layout_line"
 android:layout_width="10dp"
 android:layout_height="1dp"
 android:layout_centerInParent="true"
 android:background="#35C1B5" />
 <TextView
 android:id="@+id/tv_startime"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_toLeftOf="@+id/layout_line"
 android:layout_marginRight="22.5dp"
 android:textColor="#35C1B5"
 android:textSize="14dp"
 android:text="@string/starTime"
 />
 <TextView
 android:id="@+id/tv_endtime"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_toRightOf="@+id/layout_line"
 android:layout_marginLeft="22.5dp"
 android:textColor="#35C1B5"
 android:textSize="14dp"
 android:text="@string/endTime"
 />
 </RelativeLayout>
 <FrameLayout
 android:layout_width="match_parent"
 android:layout_height="0.5dp"
 android:layout_marginTop="10dp"
 android:background="@color/bg_line"
 />
 <com.包名.CalendarView
 android:id="@+id/calendarview"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 app:titleColor = "@color/text_lable"
 />
</LinearLayout>

SelectTimeActivity.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
public class SelectTimeActivity extends BaseActivity {
 
 @BindView(R.id.tv_title)
 TextView tvTitle;
 @BindView(R.id.iv_title_back)
 ImageView ivTitleBack;
 @BindView(R.id.tv_title_left)
 TextView tvTitleLeft;
 @BindView(R.id.layout_title_left)
 RelativeLayout layoutTitleLeft;
 @BindView(R.id.tv_title_right)
 TextView tvTitleRight;
 @BindView(R.id.layout_title_right)
 RelativeLayout layoutTitleRight;
 @BindView(R.id.layout_line)
 FrameLayout layoutLine;
 @BindView(R.id.tv_startime)
 TextView tvStartime;
 @BindView(R.id.tv_endtime)
 TextView tvEndtime;
 @BindView(R.id.calendarview)
 CalendarView calendarview;
 private String starTime;
 private String endTime;
 private boolean isSelecgOk = false;
 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_selectdate);
 ButterKnife.bind(this);
 setStatusBar(true);
 initView();
 }
 
 private void initView() {
 tvTitle.setText(getString(R.string.selectTime));
 ivTitleBack.setVisibility(View.GONE);
 tvTitleLeft.setText(getString(R.string.cancel));
 tvTitleRight.setText(getString(R.string.confirm));
 calendarview.setETimeSelListener(new CalendarView.CalendatEtimSelListener() {
 @Override
 public void onETimeSelect(Date date) {
 if (date != null) {
 endTime = DateUtils.formatData(date, Constant.TFORMATE_YMD);
 tvEndtime.setText(endTime);
 }else {
 endTime = null;
 }
 }
 });
 calendarview.setSTimeSelListener(new CalendarView.CalendarSTimeSelListener() {
 @Override
 public void onSTimeSelect(Date date) {
 if (date != null) {
 starTime = DateUtils.formatData(date, Constant.TFORMATE_YMD);
 tvStartime.setText(starTime);
 }else {
 starTime = null;
 }
 }
 });
 calendarview.setCalendaSelListener(new CalendarView.CalendaSelListener() {
 @Override
 public void selectStatus(boolean isOk) {
 isSelecgOk = isOk;
 }
 });
 }
 
 @OnClick({R.id.tv_title_left, R.id.tv_title_right})
 public void onClick(View view) {
 switch (view.getId()) {
 case R.id.tv_title_left:
 finish();
 break;
 case R.id.tv_title_right:
 if(TextUtils.isEmpty(starTime)){
 ToastUtils.showToast(getString(R.string.history_alert1));
 return;
 }
 if(TextUtils.isEmpty(endTime) || !isSelecgOk){
 ToastUtils.showToast(getResources().getString(R.string.history_alert));
 return;
 }
 Intent intent = new Intent();
 intent.putExtra("starTime",starTime);
 intent.putExtra("endTime",endTime);
 setResult(RESULT_OK,intent);
 finish();
 break;
 }
 }
}

RecyclerAdapter引用

?
1
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'

到此这篇关于Android 自定义日期段选择控件,开始时间-结束时间。的文章就介绍到这了,更多相关Android 自定义日期段选择控件,开始时间-结束时间。内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/mxy19891106/article/details/105653922

延伸 · 阅读

精彩推荐