1、效果图(两个DatePicker放在一起,同时选择起始与结束时间):

2、实现

2.1布局文件:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:gravity="center_horizontal"
  7. android:orientation="vertical"
  8. android:paddingTop="10dp">
  9.  
  10. <ScrollView
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent">
  13.  
  14. <LinearLayout
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:background="@drawable/time"
  18. android:orientation="vertical" >
  19.  
  20. <LinearLayout
  21. android:layout_width="match_parent"
  22. android:layout_height="match_parent"
  23. android:gravity="center_horizontal"
  24. android:orientation="vertical"
  25. android:padding="5dip">
  26.  
  27. <TextView
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_marginBottom="3dp"
  31. android:background="@color/white"
  32. android:textSize="18dp"
  33. android:textStyle="bold"
  34. android:textColor="@color/black"
  35. android:text="开始日期" />
  36.  
  37. <DatePicker
  38. android:id="@+id/datePickerStart"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:calendarViewShown="false" />
  42. </LinearLayout>
  43.  
  44. <ImageView
  45. android:id="@+id/imageView2"
  46. android:background="@color/white"
  47. android:layout_width="match_parent"
  48. android:layout_height="20dp"
  49. app:srcCompat="@drawable/redbound" />
  50.  
  51. <LinearLayout
  52. android:layout_width="match_parent"
  53. android:layout_height="match_parent"
  54. android:gravity="center_horizontal"
  55. android:orientation="vertical"
  56. android:padding="5dip">
  57.  
  58. <TextView
  59. android:layout_width="wrap_content"
  60. android:layout_height="wrap_content"
  61. android:layout_marginBottom="3dp"
  62. android:background="@color/white"
  63. android:textSize="18dp"
  64. android:textColor="@color/black"
  65.  
  66. android:textStyle="bold"
  67. android:text="结束日期" />
  68.  
  69. <DatePicker
  70. android:id="@+id/datePickerEnd"
  71. android:layout_width="wrap_content"
  72. android:layout_height="wrap_content"
  73. android:calendarViewShown="false" />
  74. </LinearLayout>
  75. </LinearLayout>
  76. </ScrollView>
  77.  
  78. </LinearLayout>
2.2 自定义类
  1. package com.easson.mx.pieceworkqc;
  2.  
  3. import android.app.AlertDialog;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.content.DialogInterface.OnClickListener;
  7. import android.os.Bundle;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.widget.DatePicker;
  11. import android.widget.DatePicker.OnDateChangedListener;
  12. import java.lang.reflect.Field;
  13.  
  14. /**
  15. * Created by zhangtao on 2017/11/21.
  16. */
  17. public class DoubleDatePickerDialog extends AlertDialog implements OnClickListener, OnDateChangedListener {
  18.  
  19. private static final String START_YEAR = "start_year";
  20. private static final String END_YEAR = "end_year";
  21. private static final String START_MONTH = "start_month";
  22. private static final String END_MONTH = "end_month";
  23. private static final String START_DAY = "start_day";
  24. private static final String END_DAY = "end_day";
  25.  
  26. private final DatePicker mDatePicker_start;
  27. private final DatePicker mDatePicker_end;
  28. private final OnDateSetListener mCallBack;
  29.  
  30. /**
  31. * The callback used to indicate the user is done filling in the date.
  32. */
  33. public interface OnDateSetListener {
  34. void onDateSet(DatePicker startDatePicker, int startYear, int startMonthOfYear, int startDayOfMonth,
  35. DatePicker endDatePicker, int endYear, int endMonthOfYear, int endDayOfMonth);
  36. void onConfirmed(DatePicker startDatePicker, int startYear, int startMonthOfYear, int startDayOfMonth,
  37. DatePicker endDatePicker, int endYear, int endMonthOfYear, int endDayOfMonth) ;
  38. }
  39.  
  40. /**
  41. * @param context
  42. * The context the dialog is to run in.
  43. * @param callBack
  44. * How the parent is notified that the date is set.
  45. * @param year
  46. * The initial year of the dialog.
  47. * @param monthOfYear
  48. * The initial month of the dialog.
  49. * @param dayOfMonth
  50. * The initial day of the dialog.
  51. */
  52. public DoubleDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
  53. this(context, 0, callBack, year, monthOfYear, dayOfMonth);
  54. }
  55.  
  56. public DoubleDatePickerDialog(Context context, int theme, OnDateSetListener callBack, int year, int monthOfYear,
  57. int dayOfMonth) {
  58. this(context, 0, callBack, year, monthOfYear, dayOfMonth, true);
  59. }
  60.  
  61. /**
  62. * @param context
  63. * The context the dialog is to run in.
  64. * @param theme
  65. * the theme to apply to this dialog
  66. * @param callBack
  67. * How the parent is notified that the date is set.
  68. * @param year
  69. * The initial year of the dialog.
  70. * @param monthOfYear
  71. * The initial month of the dialog.
  72. * @param dayOfMonth
  73. * The initial day of the dialog.
  74. */
  75. public DoubleDatePickerDialog(Context context, int theme, OnDateSetListener callBack, int year, int monthOfYear,
  76. int dayOfMonth, boolean isDayVisible) {
  77. super(context, theme);
  78. mCallBack = callBack;
  79.  
  80. Context themeContext = getContext();
  81. setButton(BUTTON_POSITIVE, "确 定", this);
  82. setButton(BUTTON_NEGATIVE, "取 消", this);
  83. setIcon(0);
  84.  
  85. LayoutInflater inflater = (LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  86. View view = inflater.inflate(R.layout.doubledatepicker, null);
  87. setView(view);
  88. mDatePicker_start = (DatePicker) view.findViewById(R.id.datePickerStart);
  89. mDatePicker_end = (DatePicker) view.findViewById(R.id.datePickerEnd);
  90. mDatePicker_start.init(year, monthOfYear, dayOfMonth, this);
  91. mDatePicker_end.init(year, monthOfYear, dayOfMonth, this);
  92. // 如果要隐藏当前日期(day),则使用下面方法。
  93. if (!isDayVisible) {
  94. hidDay(mDatePicker_start);
  95. hidDay(mDatePicker_end);
  96. }
  97. }
  98.  
  99. /**
  100. * 隐藏DatePicker中的日期显示
  101. * @param mDatePicker
  102. */
  103. private void hidDay(DatePicker mDatePicker) {
  104. Field[] datePickerfFields = mDatePicker.getClass().getDeclaredFields();
  105. for (Field datePickerField : datePickerfFields) {
  106. if ("mDaySpinner".equals(datePickerField.getName())) {
  107. datePickerField.setAccessible(true);
  108. Object dayPicker = new Object();
  109. try {
  110. dayPicker = datePickerField.get(mDatePicker);
  111. } catch (IllegalAccessException e) {
  112. e.printStackTrace();
  113. } catch (IllegalArgumentException e) {
  114. e.printStackTrace();
  115. }
  116. ((View) dayPicker).setVisibility(View.GONE);
  117. }
  118. }
  119. }
  120. public void onClick(DialogInterface dialog, int which) {
  121. if (which == BUTTON_NEGATIVE){
  122. tryNotifyDateSet();
  123. }
  124. //如果是“确 定”按钮,则往下执行
  125. else if(which==BUTTON_POSITIVE)
  126. {
  127. tryConfirmBtnEvent();
  128. }
  129. }
  130.  
  131. @Override
  132. public void onDateChanged(DatePicker view, int year, int month, int day) {
  133. if (view.getId() == R.id.datePickerStart) {
  134. mDatePicker_start.init(year, month, day, this);
  135. }
  136. if (view.getId() == R.id.datePickerEnd) {
  137. mDatePicker_end.init(year, month, day, this);
  138. }
  139. }
  140.  
  141. /**
  142. * 获得开始日期的DatePicker
  143. * @return The calendar view.
  144. */
  145. public DatePicker getDatePickerStart() {
  146. return mDatePicker_start;
  147. }
  148.  
  149. /**
  150. * 获得结束日期的DatePicker
  151. * @return The calendar view.
  152. */
  153. public DatePicker getDatePickerEnd() {
  154. return mDatePicker_end;
  155. }
  156.  
  157. /**
  158. * Sets the start date.
  159. *
  160. * @param year
  161. * The date year.
  162. * @param monthOfYear
  163. * The date month.
  164. * @param dayOfMonth
  165. * The date day of month.
  166. */
  167. public void updateStartDate(int year, int monthOfYear, int dayOfMonth) {
  168. mDatePicker_start.updateDate(year, monthOfYear, dayOfMonth);
  169. }
  170.  
  171. /**
  172. * Sets the end date.
  173. *
  174. * @param year
  175. * The date year.
  176. * @param monthOfYear
  177. * The date month.
  178. * @param dayOfMonth
  179. * The date day of month.
  180. */
  181. public void updateEndDate(int year, int monthOfYear, int dayOfMonth) {
  182. mDatePicker_end.updateDate(year, monthOfYear, dayOfMonth);
  183. }
  184.  
  185. private void tryNotifyDateSet() {
  186. if (mCallBack != null) {
  187. mDatePicker_start.clearFocus();
  188. mDatePicker_end.clearFocus();
  189. mCallBack.onDateSet(mDatePicker_start, mDatePicker_start.getYear(), mDatePicker_start.getMonth(),
  190. mDatePicker_start.getDayOfMonth(), mDatePicker_end, mDatePicker_end.getYear(),
  191. mDatePicker_end.getMonth(), mDatePicker_end.getDayOfMonth());
  192. }
  193. }
  194. private void tryConfirmBtnEvent()
  195. {
  196. if (mCallBack != null) {
  197. mCallBack.onConfirmed(mDatePicker_start, mDatePicker_start.getYear(), mDatePicker_start.getMonth(),
  198. mDatePicker_start.getDayOfMonth(), mDatePicker_end, mDatePicker_end.getYear(),
  199. mDatePicker_end.getMonth(), mDatePicker_end.getDayOfMonth());
  200. }
  201. }
  202.  
  203. @Override
  204. protected void onStop() {
  205. super.onStop();
  206. }
  207.  
  208. @Override
  209. public Bundle onSaveInstanceState() {
  210. Bundle state = super.onSaveInstanceState();
  211. state.putInt(START_YEAR, mDatePicker_start.getYear());
  212. state.putInt(START_MONTH, mDatePicker_start.getMonth());
  213. state.putInt(START_DAY, mDatePicker_start.getDayOfMonth());
  214. state.putInt(END_YEAR, mDatePicker_end.getYear());
  215. state.putInt(END_MONTH, mDatePicker_end.getMonth());
  216. state.putInt(END_DAY, mDatePicker_end.getDayOfMonth());
  217. return state;
  218. }
  219.  
  220. @Override
  221. public void onRestoreInstanceState(Bundle savedInstanceState) {
  222. super.onRestoreInstanceState(savedInstanceState);
  223. int start_year = savedInstanceState.getInt(START_YEAR);
  224. int start_month = savedInstanceState.getInt(START_MONTH);
  225. int start_day = savedInstanceState.getInt(START_DAY);
  226. mDatePicker_start.init(start_year, start_month, start_day, this);
  227.  
  228. int end_year = savedInstanceState.getInt(END_YEAR);
  229. int end_month = savedInstanceState.getInt(END_MONTH);
  230. int end_day = savedInstanceState.getInt(END_DAY);
  231. mDatePicker_end.init(end_year, end_month, end_day, this);
  232.  
  233. }
  234. }
2.3调用

在其他类中:

实现DoubleDatePickerDialog 中接口OnDateSetListener中的方法,以完成相应操作

  1. //取时间区间
  2. private void confirmTimeSpace() { Calendar c=Calendar.getInstance();
  3. new DoubleDatePickerDialog(ScrapedListActivity.this, 0, new DoubleDatePickerDialog.OnDateSetListener() {
  4. @Override
  5. public void onDateSet(DatePicker startDatePicker, int startYear, int startMonthOfYear, int startDayOfMonth,
  6. DatePicker endDatePicker, int endYear, int endMonthOfYear, int endDayOfMonth) {
    //取起始结束时间
  7. startTimeStr = startYear + SPACE_STR + (startMonthOfYear+1) + SPACE_STR + startDayOfMonth;
  8. endTimeStr = endYear + SPACE_STR + (endMonthOfYear+1) + SPACE_STR + endDayOfMonth;
  9.  
  10. tstartTimeStr=startYear + TSPACE_STR + (startMonthOfYear+1) + TSPACE_STR + startDayOfMonth;
  11. tendTimeStr=endYear + TSPACE_STR + (endMonthOfYear+1) + TSPACE_STR + endDayOfMonth;
    //要做的事
  12. ScrapedListActivity.this.finish();
  13. }
  14. @Override
  15. public void onConfirmed(DatePicker startDatePicker, int startYear, int startMonthOfYear, int startDayOfMonth,
  16. DatePicker endDatePicker, int endYear, int endMonthOfYear, int endDayOfMonth) {
    //取起始结束时间
  17. startTimeStr = startYear + SPACE_STR + (startMonthOfYear+1) + SPACE_STR + startDayOfMonth;
  18. endTimeStr = endYear + SPACE_STR + (endMonthOfYear+1) + SPACE_STR + endDayOfMonth;
  19.  
  20. tstartTimeStr=startYear + TSPACE_STR + (startMonthOfYear+1) + TSPACE_STR + startDayOfMonth;
  21. tendTimeStr=endYear + TSPACE_STR + (endMonthOfYear+1) + TSPACE_STR + endDayOfMonth;
  22.  
  23. //要做的事
  24. initInfoEvent();
  25. }
  26. }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), true).show();
  27. }

android自定义日期组件之双DatePicker的更多相关文章

  1. vue自定义日期组件

    vue-datepicker 基于 vuejs 2.x 可自定义主题的日期组件 github Usage 只需要在项目中加入 calendar.vue,就可以使用了. 向组件传入 prop 即可改变 ...

  2. Android图片管理组件(双缓存+异步加载)

    转自:http://www.oschina.net/code/snippet_219356_18887?p=3#comments ImageManager2这个类具有异步从网络下载图片,从sd读取本地 ...

  3. Ionic4.x 中的 UI 组件(UI Components) 日期组件

    1.日期组件的基本使用 官方文档:https://ionicframework.com/docs/api/datetime 模板中: <ion-datetime display-format=& ...

  4. Android自定义组件系列【4】——自定义ViewGroup实现双侧滑动

    在上一篇文章<Android自定义组件系列[3]--自定义ViewGroup实现侧滑>中实现了仿Facebook和人人网的侧滑效果,这一篇我们将接着上一篇来实现双面滑动的效果. 1.布局示 ...

  5. 【Android 应用开发】Android - 时间 日期相关组件

    源码下载地址 : -- CSDN :  http://download.csdn.net/detail/han1202012/6856737 -- GitHub : https://github.co ...

  6. Android - 时间 日期相关组件

    源码下载地址 : -- CSDN :  http://download.csdn.net/detail/han1202012/6856737 -- GitHub : https://github.co ...

  7. Android自定义组件

    [参考的原文地址] http://blog.csdn.net/l1028386804/article/details/47101387效果图: 实现方式: 一:自定义一个含有EditText和Butt ...

  8. Android Studio开发基础之自定义View组件

    一般情况下,不直接使用View和ViewGroup类,而是使用使用其子类.例如要显示一张图片可以用View类的子类ImageView,开发自定义View组件可分为两个主要步骤: 一.创建一个继承自an ...

  9. 9.Android之日期对话框DatePicker控件学习

    设置日期对话框在手机经常用到,今天来学习下. 首先设置好布局文件:如图 xml对应代码 <?xml version="1.0" encoding="utf-8&qu ...

随机推荐

  1. SPDY

    转载SPDY 是什么 SPDY 是 Google 开发的基于传输控制协议 (TCP) 的应用层协议 ,开发组正在推动 SPDY 成为正式标准(现为互联网草案).SPDY 协议旨在通过压缩.多路复用和优 ...

  2. 004-JVM指令集(指令码、助记符、功能描述)

    一.JVM指令助记符 1)操作数栈 变量到操作数栈:iload,iload_,lload,lload_,fload,fload_,dload,dload_,aload,aload_ 操作数栈到变量:i ...

  3. spring 项目tomcat 8.0.2 发布报错:Could not initialize class org.hibernate.validator.engine.ConfigurationImpl

    tomcat 8 项目发布遇到的错 [ERROR] -- ::, org.springframework.web.servlet.DispatcherServlet - Context initial ...

  4. JAVA项目中常用的异常知识点总结

    JAVA项目中常用的异常知识点总结 1. java.lang.nullpointerexception这个异常大家肯定都经常遇到,异常的解释是"程序遇上了空指针",简单地说就是调用 ...

  5. Python之模块和包(Day21)

    一.Python模块 Python模块(module),是一个Python文件,以.py结尾,包含了Python对象定义和Python语句. 模块让你能够有逻辑的组织你的Python代码段 把相关的代 ...

  6. Python之常用模块(re,时间,random,os,sys,序列化模块)(Day20)

    一.时间模块 #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time() 获取当前时间戳 在Python中表示时间的三种方式:时间戳,元组(s ...

  7. JQuery 评分系统

              评分: ☆ ☆ ☆ ☆ ☆ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  8. [转]AOP那些学术概念—通知、增强处理连接点(JoinPoint)切面(Aspect)

    AOP那些学术概念—通知.增强处理连接点(JoinPoint)切面(Aspect) 1.我所知道的AOP 初看起来,上来就是一大堆的术语,而且还有个拉风的名字,面向切面编程,都说是OOP的一种有益补充 ...

  9. JSP Cookie状态管理

    JSP中创建与使用Cookie 创建Cookie对象 Cookie newCookie = new Cookie(String key, Object value); 写入Cookie对象 respo ...

  10. C++11 里lambda表达式的学习

    最近看到很多关于C++11的文档,有些是我不怎么用到,所以就略过去了,但是lambda表达式还是比较常用的,其实最开始学习python的时候就觉得lambda这个比较高级,为什么C++这么弱.果然C+ ...