DatePicker control and TimePicker control developed for Android

Source: Internet
Author: User

DatePicker control and TimePicker control developed for Android

The DatePicker control inherits from the FrameLayout class. The date Selection Control provides users with date data including year, month, and day, and allows users to modify the data.

The TimePicker control inherits from the FrameLayout class and displays the time of the day to the user, allowing the user to modify it.

 

 

DatePicker control method:

1. init ()

Initialize the DatePicker date Selection control, set the year, month, and number of days for initial display, and set the date change listener. Here, the monthOfYear parameter is the initial month, and the actual displayed month is the value set here plus 1.

2. OnDateChangedListener ()

Set the date listener.

3. updateDate

Update the DatePicker control. Calling this method triggers the date change listener.

 

The TimePicker control method is as follows:

1. setCurrentHour ()

Set the hour for TimePicker display.

2. setCurrentMinute ()

Set the display time of TimePicker.

3. setIs24HourView ()

The time option controls are displayed in 24 hours.

4. setOnTimeChangedListener ()

Set the time to change the listener.

 

 

Small instance:

MainActivity. java

 

Public class MainActivity extends Activity {DatePicker datePicker; TimePicker timePicker; TextView timeStr, dateStr; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); datePicker = (DatePicker) findViewById (R. id. date); timePicker = (TimePicker) findViewById (R. id. time); timeStr = (TextView) findViewById (R. id. time_str); dateStr = (TextView) findViewById (R. id. date_str); Calendar calendar = Calendar. getInstance (); int year = calendar. get (Calendar. YEAR); int monthOfYear = calendar. get (Calendar. MONTH); int dayOfMonth = calendar ar. get (Calendar. DAY_OF_MONTH); datePicker. init (year, monthOfYear, dayOfMonth, new OnDateChangedListener () {@ Overridepublic void onDateChanged (DatePicker arg0, int year, int monthOfYear, int dayOfMonth) {dateStr. setText (year + "year" + (monthOfYear + 1) + "month" + dayOfMonth + "day") ;}}); timePicker. setOnTimeChangedListener (new OnTimeChangedListener () {@ Overridepublic void onTimeChanged (TimePicker arg0, int hour, int minuter) {timeStr. setText (hour + "" + minuter + "");}});}}
Activity_main.xml

 

 

     
          
            
        
    
   
      
      
   
  
 

:

 


 

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.