Android Self-study date DatePicker, time Timepicker Selector

Source: Internet
Author: User

The date (DatePicker), Time (Timepicker) are two more easy-to-use controls that derive from the frame layout framelayout, and they provide methods based on framelayout to get the date and time selected by the current user If the program needs to get the date and time, you can listen by adding ondatechangedlistener for DatePicker, timepicker add ontimechangelistenter for listening.

Demonstrate their functionality and usage with an example that allows the user to select a date and time.

Layout/main.xml:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 android:orientation= "vertical"4 Android:layout_width= "Fill_parent"5 Android:layout_height= "Fill_parent"6     >7 8 <!--controls that display user input date and time -9 <EditTextAndroid:id= "@+id/show"Ten Android:layout_width= "Fill_parent"  One Android:layout_height= "Wrap_content" A android:editable= "false" - android:cursorvisible= "false" - Android:text= "Choose a specific time to buy this book" the     /> - <!--defining a DatePicker component - - <DatePickerAndroid:id= "@+id/datepicker" - Android:layout_width= "Match_parent"  + Android:layout_height= "260dip" - android:layout_gravity= "Center_horizontal" +     /> A <!--defining a Timepicker component - at <TimepickerAndroid:id= "@+id/timepicker" - Android:layout_width= "Match_parent"  - Android:layout_height= "160dip" - android:layout_gravity= "Center_horizontal" -     /> -      in </LinearLayout>

The above code defines three components:EditText , date, time

Main program: Com.example.choosedate.ChooseDate.java

1  Packagecom.example.choosedate;2 3 ImportJava.util.Calendar;4 5 Importandroid.support.v7.app.ActionBarActivity;6 ImportAndroid.support.v7.app.ActionBar;7 Importandroid.support.v4.app.Fragment;8 ImportAndroid.os.Bundle;9 ImportAndroid.view.LayoutInflater;Ten ImportAndroid.view.Menu; One ImportAndroid.view.MenuItem; A ImportAndroid.view.View; - ImportAndroid.view.ViewGroup; - ImportAndroid.widget.DatePicker; the ImportAndroid.widget.DatePicker.OnDateChangedListener; - ImportAndroid.widget.EditText; - ImportAndroid.widget.TimePicker; - ImportAndroid.widget.TimePicker.OnTimeChangedListener; + ImportAndroid.os.Build; -  +  Public classChoosedateextendsactionbaractivity { A  at     //defines 5 variables for the current time of a record -     Private intYear ; -     Private intmonth; -     Private intDay ; -     Private inthour; -     Private intminute; in      - @Override to     protected voidonCreate (Bundle savedinstancestate) { +         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.main); the         //Get Components *DatePicker DatePicker =(DatePicker) Findviewbyid (r.id.datepicker); $Timepicker Timepicker =(Android.widget.TimePicker) Findviewbyid (r.id.timepicker);Panax Notoginseng          -         //Gets the current year, month, day, hour, minute theCalendar C =calendar.getinstance (); +Year =C.get (calendar.year); Amonth =C.get (calendar.month); theDay =C.get (calendar.day_of_month); +hour =C.get (calendar.hour); -minute =C.get (calendar.minute); $          $         //initializes the DatePicker component and specifies the listener -Datepicker.init (year, month, day,NewOndatechangedlistener () { -              the @Override -              Public voidOndatechanged (DatePicker view,intYearintmonth,Wuyi                     intDay ) { theChoosedate. This. Year =Year ; -Choosedate. This. Month =month; WuChoosedate. This. Day =Day ; -                  About                 //Displays the current date, time $ showdate (year, month, day, hour, minute); -             } -         }); -          A         //initializing the Timepicker component to specify the listener +Timepicker.setontimechangedlistener (NewOntimechangedlistener () { the              - @Override $              Public voidOntimechanged (Timepicker view,intHourintminute) { theChoosedate. This. Hour =hour; theChoosedate. This. Minute =minute; the                 //Displays the current date, time the showdate (year, month, day, hour, minute); -             } in         }); the              the     } About      the     //defines a method for displaying the current date and time in EditText the     Private voidShowdate (intYearintMonth,int Day the,intHour,intminute) +     { -EditText show =(EditText) Findviewbyid (r.id.show); theShow.settext ("Date:" + year + "years" + month + "month")Bayi+ Day + "days" + Hour + "time" + Minute + "min"); the     } the}

1: Pre-selection

2: After selection

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.