How to use Android datepickerdialog

Source: Internet
Author: User

(i) in Android 4.0 above the system of some mobile phones (such as my test machine red rice Note (System 4.4.4 ), as well as the simulator (System 4.0 ) , when you create a time selector with the following code, the page effect

Calendar cal ==new datepickerdialog (This,new  Datepickerdialog.ondatesetlistener () {    @Override    publicvoid ondateset (DatePicker view,int year,int monthofyear,int  dayofmonth) {       System.out.println ("Ondateset~~ondateset ~ ~ ");}    , Cal.get (calendar.year), Cal.get (Calendar.month), Cal.get (Calendar.day_of_month));   

Here's the question, not on the dialog ."Cancel"Button! And, even if you close the dialog box by returning the key or clicking on an empty area,Ondatesetmethod will still be recalled, which is the code in the aboveOndateset~~Ondateset~~thisLogwill still output!

After careful testing, you will find that when you click the Back button or an empty area to close the dialog box, Ondateset will be recalled once; click " Complete " button, Ondateset will be recalled two times. Wonderful!!!

later, the test found that this code is based on theAndroid5.0of theCMThere is no problem on the system: the dialog box has"Complete"Buttons and"Cancel"button, click"Complete"whenOndatesetbe recalled once, click"Cancel"whenOndatesetwill not be recalled. This is the best effect!

(b) In fact, it is very simple to add a Cancel button to the dialog box, with the following code:

Dialog.setbutton (Dialoginterface.button_negative, "Cancel",new  Dialoginterface.onclicklistener () {    @ Override    publicvoid OnClick (dialoginterface dialog,int  which) {       System.out.println ( "cancel~~cancel~~");}    );      

Then:       

However, the problem is not resolved. Because even if you click the Cancel button, theondateset method will still be recalled. In other words, click " cancel ", click on the Phone Return button, click on the blank area of the 3 results are the same!

(iii) Solutions:

very simple, do not use Ondatesetlistener , and the direct manual setting " Complete " Buttons and " Cancel " the event for the button. The complete code is as follows:

publicvoid pickdate (view view) {Calendar cal=calendar.getinstance (); FinalDatepickerdialog Mdialog =NewDatepickerdialog ( This,NULL, Cal.get (calendar.year), Cal.get (Calendar.month), Cal.get (Calendar.day_of_month)); //Manual Settings ButtonMdialog.setbutton (dialoginterface.button_positive,"Done",NewDialoginterface.onclicklistener () {@Override publicvoid OnClick (dialoginterface dialog,intwhich) {           //get the DatePicker component on the dialog by Mdialog.getdatepicker (), then get the date informationDatePicker DatePicker=Mdialog.getdatepicker (); intYear =datepicker.getyear (); intmonth =Datepicker.getmonth (); intDay =Datepicker.getdayofmonth (); System.out.println ( Year+ "," + month + "," +Day );    }    }); //Cancel button If you do not need to set it directlyMdialog.setbutton (dialoginterface.button_negative,"Cancel",NewDialoginterface.onclicklistener () {@Override publicvoid OnClick (dialoginterface dialog,intwhich) {System.out.println ("Button_negative~~");    }    }); Mdialog.show ();} 

Source: http://www.it165.net/pro/html/201503/36757.html

How to use Android datepickerdialog

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.