Android scrolling Time picker

Source: Internet
Author: User
Tags set time

I. Overview

Scrolling time selection now seems very common, so summarize, show the effect of general, do a reference it!

Above is, you can scroll to select the date and time, because it is operating in the 5.0 system, it seems that the 5.0 system has made changes, the following "Cancel", "OK" default will not be centered display, I do not know how to let it center display, but under 5.0 system operation by default is centered display.

Two. layout files

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"android:padding= "10dip" >    <TimepickerAndroid:timepickermode= "Spinner"Android:id= "@+id/time_picker"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_margintop= "5dip"android:layout_gravity= "Center" >    </Timepicker></LinearLayout>

The above is the time selection of the layout file, very simple, but need to pay attention to a little

Android:timepickermode= "Spinner" if not added to this sentence, under 5.0 the system runs completely without any problems, but in more than 5.0 of the system runs the discovery time layout becomes a round clock, so if you do not want to display the clock, you need to add this sentence!

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"android:padding= "10dip" >    <DatePickerAndroid:datepickermode= "Spinner"Android:calendarviewshown= "false"Android:id= "@+id/date_picker"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_margintop= "5dip"android:layout_gravity= "Center" >    </DatePicker></LinearLayout>

The above is the date selection layout, the same

Android:datepickermode= "Spinner" android:calendarviewshown= "false"
These 2 words are to avoid more than 5.0 of the system running "Calendar Style", if you remove these 2 words, will become a calendar style, 5.0 The following system can remove/retain the 2 sentences

Three. Source Code section
 Public voidsettime () {//Click the time button layout to set the timeOplantime.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//Custom ControlsAlertdialog.builder Builder =NewAlertdialog.builder (Getactivity ()); View View= (linearlayout) getlayoutinflater (). Inflate (R.layout.time_dialog,NULL); FinalTimepicker Timepicker =(Timepicker) View.findviewbyid (R.id.time_picker); //Initialization TimeCalendar.settimeinmillis (System.currenttimemillis ()); Timepicker.setis24hourview (true);                Timepicker.setcurrenthour (Calendar.get (calendar.hour_of_day));                Timepicker.setcurrentminute (Calendar.minute); //set the time layoutBuilder.setview (view); Builder.settitle ("Set Time Information"); Builder.setpositivebutton ("OK",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {Mhour=Timepicker.getcurrenthour (); Mminute=Timepicker.getcurrentminute (); //the number of times less than 10 is preceded by 0, such as 01:12:00Oplantime.settext (NewStringBuilder (). Append (Mhour < 10? "0" + mhour:mhour). Append (":"). Append (Mminute< 10? "0" + mminute:mminute). Append (": 00") );                    Dialog.cancel ();                }                }); Builder.setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich)                    {Dialog.cancel ();                }                });            Builder.create (). Show ();    }        }); }     Public voidsetDate () {//Click on the "Date" button layout to set the dateOplandate.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//implemented by custom control AlertdialogAlertdialog.builder Builder =NewAlertdialog.builder (Getactivity ()); View View= (linearlayout) getlayoutinflater (). Inflate (R.layout.date_dialog,NULL); FinalDatePicker DatePicker =(DatePicker) View.findviewbyid (R.id.date_picker); //set the date to show otherwise the detailed display includes: Week \ WeekDatepicker.setcalendarviewshown (false); //Initialize Current dateCalendar.settimeinmillis (System.currenttimemillis ()); Datepicker.init (Calendar.get (calendar.year), Calendar.get (Calendar.month), Calendar.get (Calendar.DA Y_of_month),NULL); //Set the date layoutBuilder.setview (view); Builder.settitle ("Set Date information"); Builder.setpositivebutton ("OK",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {                        //Date FormatStringBuffer SB =NewStringBuffer (); Sb.append (String.Format ("%d-%02d-%02d", Datepicker.getyear (), Datepicker.getmonth ()+ 1, Datepicker.getdayofmonth ()));                        Oplandate.settext (SB); //assign a value behind the alarm to useMyear =datepicker.getyear (); Mmonth=Datepicker.getmonth (); Mday=Datepicker.getdayofmonth ();                    Dialog.cancel ();                }                }); Builder.setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich)                    {Dialog.cancel ();                }                });            Builder.create (). Show ();    }        }); }

The code is simple, just add a click event to the date and time Button in the activity.

Datebtn.setonclicklistener (SetDate ());

Timebtn.setonclicklistener (SetTime ());



Android scrolling Time picker

Related Article

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.