DatePicker and Timepicker monitoring events get the user select Year Month day seconds/minute information

Source: Internet
Author: User

public class Mainactivity extends Appcompatactivity {private Timepicker timepicker;    Private DatePicker DatePicker;    Private Calendar cal;    private int year;    private int month;    private int day;    private int hour;    private int minute;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Registered Object Timepicker = (timepicker) Findviewbyid (R.id.timepicker);        DatePicker = (datePicker) Findviewbyid (R.id.datepicker);        Gets the month day seconds information cal = Calendar.getinstance ();        Year = Cal.get (calendar.year);        month = Cal.get (calendar.month); Day = Cal.get (calendar.day_of_month) + 1;        The month starts from 0 so add 1 hour = Cal.get (Calendar.hour_of_day);        minute = Cal.get (Calendar.minute);        Title Display Settitle (year + "-" + month + "-" + Day + "-" + Hour + ":" + minute);        DatePicker initialization Set Initial dateDatepicker.init (year, Cal.get (Calendar.month), Day, new Datepicker.ondatechangedlistener () {@Override public void ondatechanged (DatePicker view, int year, int monthofyear, int. dayofmonth) {Settitle (year +            "-" + (Monthofyear + 1) + "-" + dayofmonth);    }        }); }

Timepicker Monitoring Event Example

        Timepicker.setontimechangedlistener (New Timepicker.ontimechangedlistener () {            @Override public            Void Ontimechanged (timepicker view, int hourofday, int minute) {                                settitle (hourofday + ":" + minute);            }        );

Simultaneous monitoring of the date and time can be an alarm clock

public class Mainactivity extends Appcompatactivity {private Timepicker timepicker;    Private DatePicker DatePicker;    Private Calendar cal;    private int year;    private int month;    private int day;    private int hour;    private int Minute;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Registered Object Timepicker = (timepicker) Findviewbyid (R.id.timepicker);        DatePicker = (datePicker) Findviewbyid (R.id.datepicker);        Gets the month day seconds information cal = Calendar.getinstance ();        Year = Cal.get (calendar.year);     month = Cal.get (calendar.month) + 1;        The month starts from 0 so add 1 day = Cal.get (calendar.day_of_month);        hour = Cal.get (Calendar.hour_of_day);        Minute = Cal.get (Calendar.minute);        Title Display Settitle (year + "-" + month + "-" + Day + "-" + Hour + ":" + Minute);  DatePicker initialization Set Initial date      Datepicker.init (year, Cal.get (Calendar.month), Day, new Datepicker.ondatechangedlistener () {@Override public void ondatechanged (DatePicker view, int year, int monthofyear, int dayofmonth) {month = mo                Nthofyear + 1;                Day = DayOfMonth;                Year = year;            Settitle (Year + "-" + month + "-" + Day + "-" + Hour + ":" + Minute);        }        }); Timepicker.setontimechangedlistener (New Timepicker.ontimechangedlistener () {@Override public void O                Ntimechanged (timepicker view, int hourofday, int minute) {hour = Hourofday;                Minute = Minute;            Settitle (Year + "-" + month + "-" + Day + "-" + Hour + ":" + Minute);    }        }); }

  

DatePicker and Timepicker monitoring events get the user select Year Month day seconds/minute information

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.