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
|
package guanaj.com.easydatepickerdialog;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import java.lang.reflect.Field;
import java.util.Calendar;
/**
* Created by guanaj on 2017/3/7.
*/
class EasyDatePickerDialog extends AlertDialog implements DialogInterface.OnClickListener,
DatePicker.OnDateChangedListener {
private static final String YEAR = "year";
private static final String MONTH = "month";
private static final String DAY = "day";
private final DatePicker mDatePicker;
private final OnDateSetListener mDateSetListener;
private final Calendar mCalendar;
private boolean mHidDay =false;
private Context mContext;
public interface OnDateSetListener {
void onDateSet(DatePicker startDatePicker, int startYear, int startMonthOfYear, int startDayOfMonth);
}
private boolean mTitleNeedsUpdate = true;
public EasyDatePickerDialog(Context context,
OnDateSetListener callBack,
int year,
int monthOfYear,
int dayOfMonth,boolean hidDay) {
this(context, 0, callBack, year, monthOfYear, dayOfMonth,hidDay);
}
public EasyDatePickerDialog(Context context, int theme, OnDateSetListener listener, int year,
int monthOfYear, int dayOfMonth,boolean hidDay) {
super(context, theme);
mDateSetListener = listener;
mCalendar = Calendar.getInstance();
mHidDay = hidDay;
mContext = getContext();
final LayoutInflater inflater = LayoutInflater.from(mContext);
final View view = inflater.inflate(R.layout.easydatepickterview, null);
setButton(BUTTON_POSITIVE, "确定", this);
setButton(BUTTON_NEGATIVE, "取消", this);
//setIcon(0); //移除按钮效果
setView(view);
mDatePicker = (DatePicker) view.findViewById(R.id.easydatePicker);
mDatePicker.init(year, monthOfYear, dayOfMonth, this);
if (hidDay) {
hideDayView(mDatePicker);
}
}
private void hideDayView(DatePicker mDatePicker) {
try {
/* android5.0以上 */
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
if (daySpinnerId != 0) {
View daySpinner = mDatePicker.findViewById(daySpinnerId);
if (daySpinner != null) {
daySpinner.setVisibility(View.GONE);
}
}
} else {
Field[] datePickerfFields = mDatePicker.getClass().getDeclaredFields();
for (Field datePickerField : datePickerfFields) {
if ("mDaySpinner".equals(datePickerField.getName()) || ("mDayPicker").equals(datePickerField.getName())) {
datePickerField.setAccessible(true);
Object dayPicker = new Object();
try {
dayPicker = datePickerField.get(mDatePicker);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
((View) dayPicker).setVisibility(View.GONE);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onDateChanged(DatePicker view, int year, int month, int day) {
if (view.getId()==R.id.easydatePicker) {
mDatePicker.init(year, month, day, this);
//可根据需要在此处更新title
}
}
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case BUTTON_POSITIVE:
if (mDateSetListener != null) {
// Clearing focus forces the dialog to commit any pending
// changes, e.g. typed text in a NumberPicker.
mDatePicker.clearFocus();
mDateSetListener.onDateSet(mDatePicker, mDatePicker.getYear(),
mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
}
break;
case BUTTON_NEGATIVE:
cancel();
break;
}
}
public DatePicker getDatePicker() {
return mDatePicker;
}
public void updateDate(int year, int monthOfYear, int dayOfMonth) {
mDatePicker.updateDate(year, monthOfYear, dayOfMonth);
}
@Override
public Bundle onSaveInstanceState() {
final Bundle state = super.onSaveInstanceState();
state.putInt(YEAR, mDatePicker.getYear());
state.putInt(MONTH, mDatePicker.getMonth());
state.putInt(DAY, mDatePicker.getDayOfMonth());
return state;
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
final int year = savedInstanceState.getInt(YEAR);
final int month = savedInstanceState.getInt(MONTH);
final int day = savedInstanceState.getInt(DAY);
mDatePicker.init(year, month, day, this);
}
public void setMaxDate(int year,int month,int day){
mCalendar.set(year,month-1,day);
mDatePicker.setMaxDate(mCalendar.getTimeInMillis());
}
public void setMinDate(int year,int month,int day){
mCalendar.set(year,month-1,day);
mDatePicker.setMinDate(mCalendar.getTimeInMillis());
}
}
|