AndroidUI control series: DatePicker and TimePicker (date and time selection)

Source: Internet
Author: User
Date and time are functions available on any mobile phone platform, as is Android. Date and time are functions available on any mobile phone platform, as is Android.

DatePicker: used for date (year, month, day)

TimePicker: used for time (hours, minutes, and seconds)
Calendar: an abstract base class that converts an annual date object to an integer field, such as month, day, or hour.

For example

Final Calendar calendar = Calendar. getInstance (); mYear = calendar. get (Calendar. YEAR);-obtain the YEAR mMonth = calendar ar. get (Calendar. MONTH);-get MONTH mDay = calendar ar. get (Calendar. DAY_OF_MONTH);-get day mHour = calendar. get (Calendar. HOUR_OF_DAY);-mMinute = calendar. get (Calendar. MINUTE);-get the score

For more information, see the API documentation.

TimePickerDialog and DatePickerDialog are time classes in the form of dialog boxes.

The example is as follows:

DateTest. java file

Package org. loulijun. datetest; import java. util. calendar; import android. app. activity; import android. app. datePickerDialog; import android. app. timePickerDialog; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. datePicker; import android. widget. textView; import android. widget. timePicker; public class DateTest extends Activity {/** Called when the activity is first created. */TextView textview; TimePicker timepicker; DatePicker datepicker; Button btn1; Button btn2; // Calendar class Calendar c in JAVA; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main);/* Like other locale-sensitive classes, Calendar provides a class method, getInstance, * for getting a default instance of this class for general use. * Calendar's getInstance method returns a calendar whose locale is based on system settings * and whose time fields have been initialized with the current date and time: */c = Calendar. getInstance (); textview = (TextView) findViewById (R. id. textview); btn1 = (Button) findViewById (R. id. button1); btn2 = (Button) findViewById (R. id. button2); // Obtain the DatePicker object datepicker = (DatePicker) findViewById (R. id. datepicker1); // initialize the calendar to the current system time and set its event listening datepicker. init (c. get (Calendar. YEAR), c. get (Calendar. MONTH), c. get (Calendar. DAY_OF_MONTH), new DatePicker. onDateChangedListener () {@ Override public void onDateChanged (DatePicker view, int year, int monthOfYear, int dayOfMonth) {// TODO Auto-generated method stub // when the current date is changed, set c. set (year, monthOfYear, dayOfMonth) ;}}); // Obtain the TimePicker object timepicker = (TimePicker) findViewById (R. id. timepicker1); // Set it to the 24-hour display time. setIs24HourView (true); // The monitoring time changes to timepicker. setOnTimeChangedListener (new TimePicker. onTimeChangedListener () {@ Override public void onTimeChanged (TimePicker view, int hourOfDay, int minute) {// TODO Auto-generated method stub // Time change processing // c. set (year, month, hourOfDay, minute, second) ;}}; btn1.setOnClickListener (new Button. onClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub new DatePickerDialog (DateTest. this, new DatePickerDialog. onDateSetListener () {@ Override public void onDateSet (DatePicker view, int year, int monthOfYear, int dayOfMonth) {// Set Calendar}, c. get (Calendar. YEAR), c. get (Calendar. MONTH), c. get (Calendar. DAY_OF_MONTH )). show () ;}}); btn2.setOnClickListener (new Button. onClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub new TimePickerDialog (DateTest. this, new TimePickerDialog. onTimeSetListener () {@ Override public void onTimeSet (TimePicker view, int hourOfDay, int minute) {// TODO Auto-generated method stub // Set time}, c. get (Calendar. HOUR_OF_DAY), c. get (Calendar. MINUTE), true ). show ();}});}}

Main. xml file

 
 
  
  
  
  
  
 

The running result is as follows:

The above is the content of the Android UI control series: DatePicker and TimePicker (date and time selection). For more information, see The PHP Chinese website (www.php1.cn )!

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.