Since I wrote my blog, I have always felt too cumbersome, too troublesome, and easy to report errors and maintain. In addition, I think it is external, not from the perspective of components, after repeated consideration, I decided to change it, streamline it, and optimize it.
1. There are two simple textviews, namely, date and time. The time is written in disorder and the date is today.
2. When I click textview of the date, a dialog box is displayed, datepickerdialog, because today is July 22, August 14. When I click, the "-" And the components with numbers displayed in the middle are set to an inaccessible state, forcing users not to select the previous day, and they can only select the days later.
3. After the date is selected, click the textview of the time, and a time dialog box will pop up, timepickerdialog. When I select the previous time, the display and timepicker will be initialized to the current time.
4. You can see that in step 3, is selected. Because the time expires, textview and timepicker are initialized to the current time.
5. If it is a non-expiration time and the coming time, it will not be initialized.
6. If the date is the day after today, the time can be set regardless of the time.
Here we overwrite datepickerdialog:
/*** If the value of type is 1, the expiration date is later, 2 indicates that the previous date is expired * @ author David **/public class mydatepicker extends datepickerdialog {private int type =-1; Public mydatepicker (context, ondatesetlistener callback, int year, int monthofyear, int dayofmonth) {super (context, callback, year, monthofyear, dayofmonth);} public mydatepicker (context, ondatesetlistener callback, int year, int monthofyear, int day Ofmonth, int type) {super (context, callback, year, monthofyear, dayofmonth); this. type = type ;}@ overridepublic void ondatechanged (datepicker view, int year, int month, int day) {date time = new date (); // current date nexttime = new date (); // date to be set, pass the value to nexttime. setyear (year-1900); nexttime. setmonth (month); nexttime. setdate (day); If (view! = NULL) {If (type = 1) {If (nexttime. gettime ()-time. gettime ()> = 0) {(viewgroup) view. getchildat (0 )). getchildat (0 )). getchildat (0 ). setenabled (false); (viewgroup) view. getchildat (0 )). getchildat (1 )). getchildat (0 ). setenabled (false); (viewgroup) view. getchildat (0 )). getchildat (2 )). getchildat (0 ). setenabled (false); (viewgroup) view. getchildat (0 )). getchildat (0 )). getchildat (1 ). setenabled (false); (viewgroup) view. getchildat (0 )). getchildat (1 )). getchildat (1 ). setenabled (false); (viewgroup) view. getchildat (0 )). getchildat (2 )). getchildat (1 ). setenabled (false); super. updatedate (time. getyear () + 1900, time. getmonth (), (time. getdate ();} else {(viewgroup) view. getchildat (0 )). getchildat (0 )). getchildat (0 ). setenabled (true); (viewgroup) view. getchildat (0 )). getchildat (1 )). getchildat (0 ). setenabled (true); (viewgroup) view. getchildat (0 )). getchildat (2 )). getchildat (0 ). setenabled (true); (viewgroup) view. getchildat (0 )). getchildat (0 )). getchildat (1 ). setenabled (true); (viewgroup) view. getchildat (0 )). getchildat (1 )). getchildat (1 ). setenabled (true); (viewgroup) view. getchildat (0 )). getchildat (2 )). getchildat (1 ). setenabled (true); super. ondatechanged (view, year, month, day) ;}} else if (type = 2) {If (nexttime. gettime ()-time. gettime () <= 0) {(viewgroup) view. getchildat (0 )). getchildat (0 )). getchildat (2 ). setenabled (false); (viewgroup) view. getchildat (0 )). getchildat (1 )). getchildat (2 ). setenabled (false); (viewgroup) view. getchildat (0 )). getchildat (2 )). getchildat (2 ). setenabled (false); (viewgroup) view. getchildat (0 )). getchildat (0 )). getchildat (1 ). setenabled (false); (viewgroup) view. getchildat (0 )). getchildat (1 )). getchildat (1 ). setenabled (false); (viewgroup) view. getchildat (0 )). getchildat (2 )). getchildat (1 ). setenabled (false); super. updatedate (time. getyear () + 1900, time. getmonth (), (time. getdate (); // update pickersuper. ondatechanged (view, year, month, day);} else {(viewgroup) view. getchildat (0 )). getchildat (0 )). getchildat (2 ). setenabled (true); (viewgroup) view. getchildat (0 )). getchildat (1 )). getchildat (2 ). setenabled (true); (viewgroup) view. getchildat (0 )). getchildat (2 )). getchildat (2 ). setenabled (true); (viewgroup) view. getchildat (0 )). getchildat (0 )). getchildat (1 ). setenabled (true); (viewgroup) view. getchildat (0 )). getchildat (1 )). getchildat (1 ). setenabled (true); (viewgroup) view. getchildat (0 )). getchildat (2 )). getchildat (1 ). setenabled (true); super. updatedate (year, month, day); super. ondatechanged (view, year, month, day); // update title }}}}}
Of course, if it's just a date, you can do it without considering the time. Now, if I want to consider the time, I would have planned to rewrite the time dialog box in the same way as the rewrite date dialog box, but I found that, the re-assignment of timepicker In the rewrite always reports stack overflow errors. It is true that timepicker is assigned a value on the outside. The code is very strange:
Public class mytimepicker extends timepickerdialog {private Boolean isset; private int hour, minute; Public mytimepicker (context, ontimesetlistener callback, int hourofday, int minute, Boolean is24hourview, Boolean isset) {super (context, callback, hourofday, minute, is24hourview); this. isset = isset; this. hour = hourofday; this. minute = minute;} @ overridepublic void ontimechanged (timepicker view, int Hourofday, int minute) {date time = new date (); // the current date nexttime = new date (); // the date to be set, and pass the value to nexttime. sethours (hourofday); nexttime. setminutes (minute); If (view! = NULL) {If (! Isset) {log. I ("test", "date equals to today, time is limited"); If (nexttime. gettime ()-time. gettime () <= 0) {log. I ("test", "hour1:" + hourofday + ", minut:" + minute); log. I ("test", "date is equal to today, time is less than or equal to now, you cannot press the-button"); (viewgroup) view. getchildat (0 )). getchildat (0 )). getchildat (2 ). setenabled (false); (viewgroup) view. getchildat (0 )). getchildat (1 )). getchildat (2 ). setenabled (false); (viewgroup) view. getchildat (0 )). getchildat (0 )). getchildat (1 ). setenabled (false); (viewgroup) view. getchildat (0 )). getchildat (1 )). getchildat (1 ). setenabled (false); updatetime (hour, minute); // remarks 1super. ontimechanged (view, hourofday, minute);} else {log. I ("test", "date is equal to today, time is greater than now, you can press the-button"); log. I ("test", "hour2:" + time. gethours () + ", minut:" + time. getminutes (); (viewgroup) view. getchildat (0 )). getchildat (0 )). getchildat (2 ). setenabled (true); (viewgroup) view. getchildat (0 )). getchildat (1 )). getchildat (2 ). setenabled (true); (viewgroup) view. getchildat (0 )). getchildat (0 )). getchildat (1 ). setenabled (true); (viewgroup) view. getchildat (0 )). getchildat (1 )). getchildat (1 ). setenabled (true); this. hour = hourofday; this. minute = minute; super. ontimechanged (view, hourofday, minute) ;}} else {log. I ("test", "the date is later than today, and the time is randomly selected"); (viewgroup) view. getchildat (0 )). getchildat (0 )). getchildat (2 ). setenabled (true); (viewgroup) view. getchildat (0 )). getchildat (1 )). getchildat (2 ). setenabled (true); (viewgroup) view. getchildat (0 )). getchildat (0 )). getchildat (1 ). setenabled (true); (viewgroup) view. getchildat (0 )). getchildat (1 )). getchildat (1 ). setenabled (true); this. hour = hourofday; this. minute = minute; super. ontimechanged (view, hourofday, minute) ;}} public void setisset (Boolean isset) {This. isset = isset;} public void sethour (INT hour) {This. hour = hour;} public void setminute (INT minute) {This. minute = minute;} public int gethour () {return hour;} public int getminute () {return minute ;}}
NOTE 1: The logic here is as follows: when the selected time is less than the current time, set the "-" and the middle components under the timepickerdialog to gray and unavailable, NOTE 1: The function of the code is to set the value of timepicker to the current time. The source code of updatetime in the parent class is as follows:
public void updateTime(int hourOfDay, int minutOfHour) { mTimePicker.setCurrentHour(hourOfDay); mTimePicker.setCurrentMinute(minutOfHour); }
We can see that only the error code is initialized for timepicker, but an error will be reported when this component is initialized here:
After thinking for a long time, I did not know the reason, so I planned not to rewrite the timepickerdialog. Although the effect does not seem as intuitive as the date effect, it is much better than the previous one.
Let's take a look at the calling of the main class:
Hello. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubdatepickerdialog. ondatesetlistener listener = new datepickerdialog. ondatesetlistener () {@ overridepublic void ondateset (datepicker view, int year, int monthofyear, int dayofmonth) {// todo auto-generated method stubyear1 = year; month1 = monthofyear; day1 = dayofmonth; updatedisplay1 (); Flag = isnowdate (year1, month1, day1); // Note 2 }}; new mydatepicker (mainactivity. this, listener, year1, month1, day1, 2 ). show () ;}}); World. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {ontimesetlistener listener = new listener () {@ overridepublic void ontimeset (timepicker view, int hourofday, int minute) {Boolean issettime = isnowtime (flag, hourofday, minute );/ /Remarks 3if (view! = NULL) {If (! Issettime) {(viewgroup) view. getchildat (0 )). getchildat (0 )). getchildat (2 ). setenabled (false); (viewgroup) view. getchildat (0 )). getchildat (1 )). getchildat (2 ). setenabled (false); (viewgroup) view. getchildat (0 )). getchildat (0 )). getchildat (1 ). setenabled (false); (viewgroup) view. getchildat (0 )). getchildat (1 )). getchildat (1 ). setenabled (false); inittimepicker (view, new date (). gethours (), new date (). getminutes (); // time1.ontimechanged (view, hourofday, minute); updatedisplay2 (new date (). gethours (), new date (). getminutes ();} else {(viewgroup) view. getchildat (0 )). getchildat (0 )). getchildat (2 ). setenabled (true); (viewgroup) view. getchildat (0 )). getchildat (1 )). getchildat (2 ). setenabled (true); (viewgroup) view. getchildat (0 )). getchildat (0 )). getchildat (1 ). setenabled (true); (viewgroup) view. getchildat (0 )). getchildat (1 )). getchildat (1 ). setenabled (true); updatedisplay2 (hourofday, minute) ;}}}; time1 = new timepickerdialog (mainactivity. this, listener, integer. parseint (World. gettext (). tostring (). split (":") [0]. trim (), integer. parseint (World. gettext (). tostring (). split (":") [1]. trim (), true); time1.show ();}});}
Note 2: A boolean value is obtained by comparing the set date with the current date to determine whether to restrict the time.
NOTE 3: A boolean value is obtained by comparing the set time with the current time to determine how to initialize the time component.
Demo download: http://download.csdn.net/detail/duancanmeng/4499365