DatePicker 1.month runs in the machine starting from 0, in the SDK method is used in no more than 1 of the way, only when used to display the manual 1;2. Gets the date instance and initializes and prints: Calendar cal=calendar.getinstance (); 3. Get current date and time: int Year=cal.get (calendar.year), int month=cal.get (calendar.month) +1;int Day=cal.get (calendar.day_of_ MONTH); int hour=cal.get (calendar.hour_of_day); int minute=cal.get (calendar.minute_of_hour); Settitle (year+ "-" + month+ "-" +day+ "-" +hour+ ":" +minute ", 4.//the date on the title can change with the selection of Datepicker.init (year,month,day,new Ondatechangedlistener ()) {@Overridepublic void ondatechanged (DatePicker view,int year,int cal.get (calendar.month_of_ year), int dayofmonth) {settitle (year+ "-" + (monthofyear+1) + "-" +dayofmonth);}}
The time picker is much simpler than the last date selector, which does not require the conversion of the month, and then does not require initialization, but directly sets the listener. Timepicker.setontimechangedlistener (New Timepicker.ontimechangedlistener () { @Override public Void Ontimechanged (timepicker timepicker, int hourofday, int minute) { settitle (hourofday+ ":" +minute); } });
Datepickerdialog Summary 1. Instantiate public Datepickerdialog (context context, //Contexts Ondatesetlistener callBack, //Return function int year, //Initial month day int monthofyear, int dayofmonth) 2. Call the. Show () method to display
1 NewDatepickerdialog ( This,NewOndatesetlistener () {2 3 @Override4 Public voidOndateset (DatePicker view,intYearintMonthofyear,5 intdayofmonth) {6 //TODO auto-generated Method Stub7Settitle (Year + "-" + (monthofyear+1) + "-" +dayofmonth);8 }9}, year, month, year). Show ();
Timepickerdialog Summary 1. Instantiate public Timepickerdialog (context context,//contexts Ontimesetlistener callBack,//return function int hourofday, int minute,//initial month day, boolean Is24hourview)//Whether 24-hour system
1 NewTimepickerdialog ( This,NewOntimesetlistener () {2 3 @Override4 Public voidOntimeset (Timepicker view,intHourofday,intminute) {5 //TODO auto-generated Method Stub6 7 }8}, hour, minute,true). Show ();
Ps:
The DatePicker object is initialized with the Init method to specify the day of the DatePicker initialization and the Ondatechangedlistener to listen for changes to the date, and Timepicker does not require init initialization. Only need to set the listening Ontimechangedlistener to listen to the change of time, Datepickerdailog is a dialog box to pop up the date selector, you need to use new way to create and pass in the corresponding listening Ondatesetlistener, Date to initialize the data, and finally remember to set the dialog box display (show) method. The same timepickerdailog also needs to be initialized in new mode, and setting Ontimelistene to note that the DatePicker initialization month may require-1 operation (if Month=calendar.get ( Calendar.month) + 1, you need to subtract one), but using Calendar.get (calendar.month) directly in Init () means that the month does not need to be done
Android Siege Lion Datepicke&timepicker