id="@+id/picktime"android:
layout_width="wrap_content"
29
android:
layout_height="wrap_content"android:
text="选择时间"/>
30
31
32
【3】包com.andyidea.calenderdemo下MainActivity.java源码:
查看源码
打印?
001
packagecom.andyidea.calenderdemo;
002
003
importjava.util.Calendar;
004
005
importandroid.app.Activity;
006
importandroid.app.DatePickerDialog;
007
importandroid.app.Dialog;
008
importandroid.app.TimePickerDialog;
009
importandroid.os.Bundle;
010
importandroid.os.Handler;
011
importandroid.os.Message;
012
importandroid.view.View;
013
importandroid.widget.Button;
014
importandroid.widget.DatePicker;
015
importandroid.widget.EditText;
016
importandroid.widget.TimePicker;
017
018
publicclassMainActivityextendsActivity{
019
020
privateEditTextshowDate=null;
021
privateButtonpickDate=null;
022
privateEditTextshowTime=null;
023
privateButtonpickTime=null;
024
025
privatestaticfinalintSHOW_DATAPICK=0;
026
privatestaticfinalintDATE_DIALOG_ID=1;
027
privatestaticfinalintSHOW_TIMEPICK=2;
028
privatestaticfinalintTIME_DIALOG_ID=3;
029
030
privateintmYear;
031
privateintmMonth;
032
privateintmDay;
033
privateintmHour;
034
privateintmMinute;
035
036
/**Calledwhentheactivityisfirstcreated.*/
037
@Override
038
publicvoidonCreate(BundlesavedInstanceState){
039
super.onCreate(savedInstanceState);
040
setContentView(R.layout.main);
041
042
initializeViews();
043
044
finalCalendarc=Calendar.getInstance();
045
mYear=c.get(Calendar.YEAR);
046
mMonth=c.get(Calendar.MONTH);
047
mDay=c.get(Calendar.DAY_OF_MONTH);
048
049
mHour=c.get(Calendar.HOUR_OF_DAY);
050
mMinute=c.get(Calendar.MINUTE);
051
052
setDateTime();
053
setTimeOfDay();
054
}
055
056
/**
057
*初始化控件和UI视图
058
*/
059
privatevoidinitializeViews(){
060
showDate=(EditText)findViewById(R.id.showdate);
061
pickDate=(Button)findViewById(R.id.pickdate);
062
showTime=(EditText)findViewById(R.id.showtime);
063
pickTime=(Button)findViewById(R.id.picktime);
064
065
pickDate.setOnClickListener(newView.OnClickListener(){
066
067
@Override
068
publicvoidonClick(Viewv){
069
Messagemsg=newMessage();
070
if(pickDate.equals((Button)v)){
071
msg.what=MainActivity.SHOW_DATAPICK;
072
}
073
MainActivity.this.dateandtimeHandler.sendMessage(msg);
074
}
075
});
076
077
pickTime.setOnClickListener(newView.OnClickListener(){
078
079
@Override
080
publicvoidonClick(Viewv){
081
Messagemsg=newMessage();
082
if(pickTime.equals((Button)v)){
083
msg.what=MainActivity.SHOW_TIMEPICK;
084
}
085
MainActivity.this.dateandtimeHandler.sendMessage(msg);
086
}
087
});
088
}
089
090
/**
091
*设置日期
092
*/
093
privatevoidsetDateTime(){
094
finalCalendarc=Calendar.getInstance();
095
096
mYear=c.get(Calendar.YEAR);
097
mMonth=c.get(Calendar.MONTH);
098
mDay=c.get(Calendar.DAY_OF_MONTH);
099
100
updateDateDisplay();
101
}
102
103
/**
104
*更新日期显示
105
*/
106
privatevoidupdateDateDisplay(){
107
showDate.setText(newStringBuilder().append(mYear).append("-")
108
.append((mMonth+1)<10?
"0"+(mMonth+1):
(mMonth+1)).append("-")
109
.append((mDay<10)?
"0"+mDay:
mDay));
110
}
111
112
/**
113
*日期控件的事件
114
*/
115
privateDatePickerDialog.OnDateSetListenermDateSetListener=newDatePickerDialog.OnDateSetListener(){
116
117
publicvoidonDateSet(DatePickerview,intyear,intmonthOfYear,
118
intdayOfMonth){
119
mYear=year;
120
mMonth=monthOfYear;
121
mDay=dayOfMonth;
122
123
updateDateDisplay();
124
}
125
};
126
127
/**
128
*设置时间
129
*/
130
privatevoidsetTimeOfDay(){
131
finalCalendarc=Calendar.getInstance();
132
mHour=c.get(Calendar.HOUR_OF_DAY);
133
mMinute=c.get(Calendar.MINUTE);
134
updateTimeDisplay();
135
}
136
137
/**
138
*更新时间显示
139
*/
140
privatevoidupdateTimeDisplay(){
141
showTime.setText(newStringBuilder().append(mHour).append(":
")
142
.append((mMinute<10)?
"0"+mMinute:
mMinute));
143
}
144
145
/**
146
*时间控件事件
147
*/
148
privateTimePickerDialog.OnTimeSetListenermTimeSetListener=newTimePickerDialog.OnTimeSetListener(){
149
150
@Override
151
publicvoidonTimeSet(TimePickerview,inthourOfDay,intminute){
152
mHour=hourOfDay;
153
mMinute=minute;
154
155
updateTimeDisplay();
156
}
157
};
158
159
@Override
160
protectedDialogonCreateDialog(intid){
161
switch(id){
162
caseDATE_DIALOG_ID:
163
returnnewDatePickerDialog(this,mDateSetListener,mYear,mMonth,
164
mDay);
165
caseTIME_DIALOG_ID:
166
returnnewTimePickerDialog(this,mTimeSetListener,mHour,mMinute,true);
167
}
168
169
returnnull;
170
}
171
172
@Override
173
protectedvoidonPrepareDialog(intid,Dialogdialog){
174
switch(id){
175
caseDATE_DIALOG_ID:
176
((DatePickerDialog)dialog).updateDate(mYear,mMonth,mDay);
177
break;
178
caseTIME_DIALOG_ID:
179
((TimePickerDialog)dialog).updateTime(mHour,mMinute);
180
break;
181
}
182
}
183
184
/**
185
*处理日期和时间控件的Handler
186
*/
187
HandlerdateandtimeHandler=newHandler(){
188
189
@Override
190
publicvoidhandleMessage(Messagemsg){
191
switch(msg.what){
192
caseMainActivity.SHOW_DATAPICK:
193
showDialog(DATE_DIALOG_ID);
194
break;
195
caseM