Android learning notes: how to set the date and time before the current time cannot be selected

Source: Internet
Author: User

Based on the business needs, you need to implement the function as shown in the following figure:

1. If the year is set to be greater than the current year, you can set the month, day, and time;

2. If the year is set to less than the current year, the setting fails;

3. If the year is set to the current year, if the month is set to be later than the current month, the day and time can be set at will;

4. If the year is set to the current year, if the month is set to a value smaller than the current month, the setting fails;

5. If the year is set to the current year, if the month is set to the current month, the time can be set if the day is set to the current day;

......

And so on.

In Android, we first think of the datepicker and timepicker components for date and time, and then the implementation method. How can we implement it? I originally imagined that datepickerdialog and timepickerdialog were used to determine whether the buttons in datepicker can be pressed. For example, if the annual value is set to be smaller than the current year, the "-" button in datepicker cannot be pressed. This method is difficult to implement, and some items in the source code are not understood, so I made a component similar to datepickerdialog, for example:

Today is the 6th day, and the "-" button is not reflected. Clicking it turns gray. It can be achieved by analyzing the above business step by step. It is not difficult, however, it is very cumbersome, especially the layout. the most annoying thing is to adjust the layout. Therefore, we should put these logics on the original datepicker for implementation. Of course, the implemented interface may not be so intuitive.

1 2

3 4

5 6

7

It may feel a little pitfall, but I will try again.

Let's look at the key code:

@ Overrideprotected dialog oncreatedialog (int id) {dialog = NULL; Switch (ID) {case R. id. wait_edit_yuyuedata: datepickerdialog. ondatesetlistener datelistener = new datepickerdialog. ondatesetlistener () {@ override public void ondateset (datepicker, int year, int month, int dayofmonth) {log. I (TAG, "ondateset -----------> year:" + year + ", month:" + (month + 1) + ", day:" + dayofmonth); If (Year> getyear () {// Set the year to be greater than the current year. You do not need to judge the following wait_edit_yuedata.settext (dayofmonth + "/" + (month + 1) + "/" + year ); flag = 1;} else if (year = getyear () {// If the year is set to the current year, the monthly if (month + 1)> getmonth () {// set the month to the current month. Set it directly without judging the flag = 1; wait_edit_yuyuedata.settext (dayofmonth + "/" + (month + 1) + "/" + year);} else if (month + 1) = getmonth () {// set the month to the current month, then start to judge the next day if (dayofmonth> getday () {// set the day to be greater than the current month, directly set, do not judge the following flag = 1; wait_ed It_yuedata.settext (dayofmonth + "/" + (month + 1) + "/" + year);} else if (dayofmonth = getday ()) {// If the day is set to the current day, the flag is set to 2; wait_edit_yuedata.settext (dayofmonth + "/" + (month + 1) + "/" + year );} else {// set the day to be less than the current day, and prompt to reset flag = 3; toast. maketext (mainactivity. this, "the current day cannot be smaller than today, Please reset", 2000 ). show () ;}} else {// set the month to be smaller than the current month, prompting you to reset flag = 3; toast. maketext (mainactivity. this, "the current month cannot be smaller than this month. Please reset", 2000 ). show () ;}} else {// If the year is set to less than the current year, the system prompts you to reset flag = 3; toast. maketext (mainactivity. this, "the current year cannot be less than this year, Please reset", 2000 ). show ();} If (flag = 3) {datepicker. init (getyear (), (getmonth ()-1), getday (), new datepicker. ondatechangedlistener () {@ overridepublic void ondatechanged (datepicker view, int year, int monthofyear, int dayofmonth) {Week (dayofmonth + "/" + (monthofyear + 1) + "/" + year) ;}}}}; dialog = new date Pickerdialog (this, datelistener, getyear (), (getmonth ()-1), getday (); break; case R. id. wait_edit_arriveltime: timepickerdialog. ontimesetlistener timelistener = new timepickerdialog. ontimesetlistener () {@ overridepublic void ontimeset (timepicker view, int hourofday, int minute) {log. I (TAG, "ontimeset -----------> flag:" + flag + ", hourofday:" + hourofday + ", minute:" + minute); Switch (FLAG) {Case 1: // set the date after the current date, Set the time directly without judging wait_edit_arriveltime.settext (hourofday + ":" + minute); break; Case 2: // set the date after the current date, judgment time and minute if (hourofday> gethonor () {wait_edit_arriveltime.settext (hourofday + ":" + minute);} else if (hourofday = gethonor ()) {If (minute> getmin () {wait_edit_arriveltime.settext (hourofday + ":" + minute);} else {wait_edit_arriveltime.settext (gethonor () + ":" + getmin ()); toast. maketext (mainactivity. this, "select minutes later than the current time", 2000 ). show () ;}} Else {wait_edit_arriveltime.settext (gethonor () + ":" + getmin (); toast. maketext (mainactivity. this, "select an hour later than the current time", 2000 ). show ();} break; Case 3: // you are prompted that the date is not correctly set before the date is set to the current date. The time wait_edit_arriveltime.settext (gethonor () + ": "+ getmin (); toast. maketext (mainactivity. this, "Please set the correct date", 2000 ). show (); break; default: Break ;}}; log. I (TAG, "init -----------> flag:" + flag + ", hourofday:" + gethonor () + ", minute:" + getmin (); Di Alog = new timepickerdialog (this, timelistener, gethonor (), getmin (), true); break; Case 1: alertdialog. builder = new alertdialog. builder (mainactivity. this); builder. settitle ("are you sure you want to stop editing? "); Builder. setpositivebutton ("OK", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface arg0, int arg1) {mainactivity. this. finish () ;}}); builder. setnegativebutton ("cancel", new dialoginterface. onclicklistener () {public void onclick (dialoginterface dialog, int whichbutton) {}}); builder. show (); break; default: break;} return dialog ;}

Specific code, please click to download: 1: http://download.csdn.net/detail/duancanmeng/4480117 2: http://download.csdn.net/detail/duancanmeng/4480126

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.