默认显示当前时间:
time.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
|
<
strong
><?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:orientation
=
"vertical"
>
<
TextView
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:gravity
=
"center"
android:text
=
"日期和时间控件的使用DEMO"
/>
<
LinearLayout
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:orientation
=
"horizontal"
>
<
EditText
android:id
=
"@+id/showdate"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:layout_weight
=
"1"
/>
<
Button
android:id
=
"@+id/pickdate"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"选择日期"
/>
</
LinearLayout
>
<
LinearLayout
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:orientation
=
"horizontal"
>
<
EditText
android:id
=
"@+id/showtime"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:layout_weight
=
"1"
/>
<
Button
android:id
=
"@+id/picktime"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"选择时间"
/>
</
LinearLayout
>
</
LinearLayout
></
strong
>
|
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
|
public
class
TimeActivity
extends
Activity {
private
EditText showDate =
null
;
private
Button pickDate =
null
;
private
EditText showTime =
null
;
private
Button pickTime =
null
;
private
static
final
int
SHOW_DATAPICK =
0
;
private
static
final
int
DATE_DIALOG_ID =
1
;
private
static
final
int
SHOW_TIMEPICK =
2
;
private
static
final
int
TIME_DIALOG_ID =
3
;
private
int
mYear;
private
int
mMonth;
private
int
mDay;
private
int
mHour;
private
int
mMinute;
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.time);
initializeViews();
final
Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
setDateTime();
setTimeOfDay();
}
/**
* 初始化控件和UI视图
*/
private
void
initializeViews(){
showDate = (EditText) findViewById(R.id.showdate);
pickDate = (Button) findViewById(R.id.pickdate);
showTime = (EditText)findViewById(R.id.showtime);
pickTime = (Button)findViewById(R.id.picktime);
pickDate.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
Message msg =
new
Message();
if
(pickDate.equals((Button) v)) {
msg.what = TimeActivity.SHOW_DATAPICK;
}
TimeActivity.
this
.dateandtimeHandler.sendMessage(msg);
}
});
pickTime.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
Message msg =
new
Message();
if
(pickTime.equals((Button) v)) {
msg.what = TimeActivity.SHOW_TIMEPICK;
}
TimeActivity.
this
.dateandtimeHandler.sendMessage(msg);
}
});
}
/**
* 设置日期
*/
private
void
setDateTime(){
final
Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDateDisplay();
}
/**
* 更新日期显示
*/
private
void
updateDateDisplay(){
showDate.setText(
new
StringBuilder().append(mYear).append(
"-"
)
.append((mMonth +
1
) <
10
?
"0"
+ (mMonth +
1
) : (mMonth +
1
)).append(
"-"
)
.append((mDay <
10
) ?
"0"
+ mDay : mDay));
}
/**
* 日期控件的事件
*/
private
DatePickerDialog.OnDateSetListener mDateSetListener =
new
DatePickerDialog.OnDateSetListener() {
public
void
onDateSet(DatePicker view,
int
year,
int
monthOfYear,
int
dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDateDisplay();
}
};
/**
* 设置时间
*/
private
void
setTimeOfDay(){
final
Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
updateTimeDisplay();
}
/**
* 更新时间显示
*/
private
void
updateTimeDisplay(){
showTime.setText(
new
StringBuilder().append(mHour).append(
":"
)
.append((mMinute <
10
) ?
"0"
+ mMinute : mMinute));
}
/**
* 时间控件事件
*/
private
TimePickerDialog.OnTimeSetListener mTimeSetListener =
new
TimePickerDialog.OnTimeSetListener() {
@Override
public
void
onTimeSet(TimePicker view,
int
hourOfDay,
int
minute) {
mHour = hourOfDay;
mMinute = minute;
updateTimeDisplay();
}
};
@Override
protected
Dialog onCreateDialog(
int
id) {
switch
(id) {
case
DATE_DIALOG_ID:
return
new
DatePickerDialog(
this
, mDateSetListener, mYear, mMonth,
mDay);
case
TIME_DIALOG_ID:
return
new
TimePickerDialog(
this
, mTimeSetListener, mHour, mMinute,
true
);
}
return
null
;
}
@Override
protected
void
onPrepareDialog(
int
id, Dialog dialog) {
switch
(id) {
case
DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break
;
case
TIME_DIALOG_ID:
((TimePickerDialog) dialog).updateTime(mHour, mMinute);
break
;
}
}
/**
* 处理日期和时间控件的Handler
*/
Handler dateandtimeHandler =
new
Handler() {
@Override
public
void
handleMessage(Message msg) {
switch
(msg.what) {
case
TimeActivity.SHOW_DATAPICK:
showDialog(DATE_DIALOG_ID);
break
;
case
TimeActivity.SHOW_TIMEPICK:
showDialog(TIME_DIALOG_ID);
break
;
}
}
};
|