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