Android: datepickerdialog cancel dialog without getting the date -
in fragment class i'm using snippet:
calendar c = calendar.getinstance(); int myear = c.get(calendar.year); int mmonth = c.get(calendar.month); int mday = c.get(calendar.day_of_month); datepickerdialog dialog = new datepickerdialog(getactivity(), datepickerlistener, myear, mmonth, mday); dialog.setbutton(datepickerdialog.button_positive, "ok", dialog); dialog.setbutton(datepickerdialog.button_negative, "annulla", (dialoginterface.onclicklistener)null); dialog.show(); private datepickerdialog.ondatesetlistener datepickerlistener = new datepickerdialog.ondatesetlistener() { // when dialog box closed, below method called. public void ondateset(datepicker view, int selectedyear, int selectedmonth, int selectedday) { //do whatever want log.d("datepicker","la data presa รจ "+selectedday+selectedmonth+selectedyear); } };
unfortunately i'm getting date both positive , negative button. of course need date positive button (i see both entry in log).
thank much!
if @ source code (tested on api 16) of datepickerdialog you'll notice ondateset() called in onstop() method. overridden default positive button behaviour, , implemented positive button listener instead ondateset()
private class reportdatepickerdialog extends dialogfragment implements android.content.dialoginterface.onclicklistener { @override public dialog oncreatedialog(bundle savedinstancestate) { datepickerdialog dialog = new datepickerdialog(getactivity(), null, initialyear, initialmonth, initialday); dialog.setcancelable(true); dialog.setcanceledontouchoutside(true); dialog.setbutton(dialoginterface.button_positive, "ok", reportdatepickerdialog.this); return dialog; } @override public void onclick(dialoginterface dialog, int which) { ((datepickerdialog)getdialog()).onclick(dialog, which); if (which != dialoginterface.button_positive) { return; } datepicker datepicker = ((datepickerdialog)getdialog()).getdatepicker(); datepicker.getdayofmonth(); datepicker.getmonth(); datepicker.getyear(); } }
i guess it's better off create custom dialog window in cases that.
Comments
Post a Comment