About datepickerdialog click back to cancel the solution of calling the callback method ondateset ()

Source: Internet
Author: User

Http://bbs.9ria.com/forum.php? MoD = viewthread & tid = 205312.





A bug was reported during the test yesterday. The content is roughly as follows: after clicking datepickerdialog in the birthday pop-up On 2 mobile phone, modify the date without clicking finish. Click Back to modify the date in textview.

After analysis, in the code, I put the judgment logic for processing the date in the ondateset () callback METHOD OF THE ondatesetlistener listener. When the logic judgment is passed, the date in the textview will be modified, this indicates that the ondateset () callback method is called when the return key is clicked.

However, the method provided by datepickerdialog does not directly control the method of clicking the return key. How do I deal with it?

My earliest thought was to use the setbutton method provided by datepickerdialog to set it as a confirmation and cancel two buttons, but this undoubtedly adds a lot of code and logic processing.

Is there a more convenient and simpler way? Yes. inherit from datepickerdialog to override the life cycle method onstop ().


  1. Class mydatepickerdialog extends datepickerdialog {

  2. Public mydatepickerdialog (context, ondatesetlistener callback,
  3. Int year, int monthofyear, int dayofmonth ){
  4. Super (context, callback, year, monthofyear, dayofmonth );
  5. }
  6. @ Override
  7. Protected void onstop (){
  8. // Super. onstop ();
  9. }
  10. }

Copy code


Careful students will surely see that I have commented out, super. onstop (). Why can I comment out this method so that the return key does not call the ondateset method?

In the source code of datepickerdialog, during the onstop () lifecycle, trypolicydateset () is called ();


  1. @ Override
  2. Protected void onstop (){
  3. Trypolicydateset ();
  4. Super. onstop ();
  5. }

Copy code


Trypolicydateset (); through the method name, you can also see that it is the ondateset () method, which means that when the datepickerdialog Life Cycle reaches onstop (), it will be called to ondateset. The trypolicydateset () method is as follows:


  1. Private void trypolicydateset (){
  2. If (mcallback! = NULL ){
  3. Mdatepicker. clearfocus ();
  4. Mcallback. ondateset (mdatepicker, mdatepicker. getyear (),
  5. Mdatepicker. getmonth (), mdatepicker. getdayofmonth ());
  6. }
  7. }

Copy code


This explains why I commented out the super method in the rewriting and achieved the goal.

Another one, I searched in advance to avoid repeated rounds, but I haven't explained the article yet. So I sent it. If something is wrong, please kindly advise.


Original Author: kenth_yo

Address: http://my.eoe.cn/786272/archive/4091.html

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.