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 ().
|
- Class mydatepickerdialog extends datepickerdialog {
-
- Public mydatepickerdialog (context, ondatesetlistener callback,
- Int year, int monthofyear, int dayofmonth ){
- Super (context, callback, year, monthofyear, dayofmonth );
- }
- @ Override
- Protected void onstop (){
- // Super. onstop ();
- }
- }
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 ();
|
- @ Override
- Protected void onstop (){
- Trypolicydateset ();
- Super. onstop ();
- }
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:
|
- Private void trypolicydateset (){
- If (mcallback! = NULL ){
- Mdatepicker. clearfocus ();
- Mcallback. ondateset (mdatepicker, mdatepicker. getyear (),
- Mdatepicker. getmonth (), mdatepicker. getdayofmonth ());
- }
- }
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 |