Android-time and date related components

Source: Internet
Author: User
Tags time and date

 

 

I. Clock Components

 

1. AnalogClock component

 

Appearance: The widget displays a dial with minute and second hands rotating;

Attribute Introduction: This component can be used to set the dial image and display the image by the hour hand;

--Set dial: Android: dial, set the R. drawable resource image;

--Set the hour hand: Android: hand_hour, set the R. drawable resource image;

--Set the sub-needle: Android: hand_minute, set the R. drawable resource image;

 

2. DigitalClock component

 

Appearance: This component is a TextView component that displays the text of the current time;

Attribute: The android: text attribute cannot be set for this component. The setting is also invalid;

 

3. source code example

 

 

 
         
      
          
           
        
   
  
 

:

 

 

Ii. Timer Chronometer

 

Format attribute: Android: format, which specifies the timer format;

Common Methods: The timer method is the focus. You can control the start and stop operations of the timer;

--Set Time: SetBase (long), set the start time;

--Set format: SetFormat (string): set the time display format;

--Start timing: Start (), start timing method;

--Stop timing: Stop (), stop timing method;

--Set listeners: SetOnChronometerTickListener (), sets a listener, and calls back this method when the timer timing changes;

 

Instance:

XML source code:

 

 
         
          
       
   
  
 

Activity Code:

 

 

Package shuliang. han. time_date_test; import android. app. activity; import android. OS. bundle; import android. OS. systemClock; import android. widget. chronometer; import android. widget. chronometer. onChronometerTickListener; import android. widget. compoundButton; import android. widget. compoundButton. onCheckedChangeListener; import android. widget. toast; import android. widget. toggleButton; public class ChronometerActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. chronometer); final Chronometer chronometer = (Chronometer) findViewById (R. id. chronometer); ToggleButton toggleButton = (ToggleButton) findViewById (R. id. toggle); toggleButton. listener (new OnCheckedChangeListener () {@ Overridepublic void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {if (isChecked) {// click stop timing from the display, checked false-> true stop timing, display start time chronometer. stop ();} else {// display start timer click, checked true-> false start timer, show stop timer chronometer. start (); chronometer. setBase (SystemClock. elapsedRealtime () ;}}); // sets a listener. When the time exceeds 5 seconds, the Toast information is chronometer. setOnChronometerTickListener (new OnChronometerTickListener () {@ Overridepublic void onChronometerTick (Chronometer chronometer) {if (SystemClock. elapsedRealtime ()-chronometer. getBase ()> 5*1000) Toast. makeText (getApplicationContext (), 5 seconds, Toast. LENGTH_LONG ). show ();}});}}

:

 

 

3. Calendar view CalendarView

 

Calendar View: The calendar view displays a 7 * N square, that is, calendar. You can set N. In the scroll view, you can select a date of another month or year, and set a date to change the listener, listen to calendar selection events;

XML attributes of the calendar View:

--Set Style: Android: dateTextAppearance. Set the date text display style;

--Set the first day: Android: firstDayOfWeek. Set the day of the week to the first day of the week. The default value is Monday;

--Selected color: Android: focusedMonthDateColor, which specifies the color of the selected month and date;

--Max date: Android: maxDate, which specifies the maximum supported dates in mm/dd/yyyy format;

--Minimum date: Android: minDate: Specifies the supported minimum date in mm/dd/yyyy format;

--Select a vertical line: Android: selectedDateVerticalBar, which is used to set the Drawable (R.drawable.int) on both sides of the selected date;

--Week color: Android: selectedWeekBackground, which specifies the background color of the week where the selected date is located;

--Weekly display: Android: showWeekNumber, which indicates whether to display the number of weeks;

--Set weeks: Android: shownWeekCount, set the calendar component to display a total of weeks;

--Unselected color: Android: unfocusedMonthDateColor, set the date color of the unselected month;

--Week Style: Android: weekDayTextAppearance. Set the text style of the day of the week;

--Week color: Android: weekNumberColor, which specifies the color of the weekly ID;

--Weekly color Division: Android: weekSeparatorLineColor. Set the color of the weekly separator line;

 

Instance:

XML source code:

 

 
         
          
       
       
   
  
 

Activity source code:

 

 

Package shuliang. han. time_date_test; import android. app. activity; import android. OS. bundle; import android. widget. calendarView; import android. widget. calendarView. onDateChangeListener; import android. widget. toast; public class CalendarActivity extends Activity {private CalendarView calendarView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. calendar); calendarView = (CalendarView) findViewById (R. id. calendarView); // sets the date change listener, which is activated when the date is changed. setOnDateChangeListener (new OnDateChangeListener () {@ Overridepublic void onSelectedDayChange (CalendarView view, int year, int month, int dayOfMonth) {Toast. makeText (getApplicationContext (), the selected date is: + year + month + dayOfMonth + day, Toast. LENGTH_LONG ). show ();}});}}

:

 

 

Iv. Time Selector

 

TimePicker allows you to select a time. The component is more elegant. You can also set a time to change the listener. Once the time changes, the callback method is triggered;

 

Instance source code:

XML file:

 

 
         
          
   
  
 

Activity source code:

 

 

Package shuliang. han. time_date_test; import java. util. calendar; import android. app. activity; import android. OS. bundle; import android. widget. datePicker; import android. widget. timePicker; import android. widget. timePicker. onTimeChangedListener; import android. widget. toast; public class TimePickerActivity extends Activity {private TimePicker timePicker; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. time_picker); timePicker = (TimePicker) findViewById (R. id. time_picker); // print the current time getCurrentDateTime (); timePicker. setOnTimeChangedListener (new OnTimeChangedListener () {@ Overridepublic void onTimeChanged (TimePicker view, int hourOfDay, int minute) {Toast. makeText (getApplicationContext (), change time: + hourOfDay + hour + minute, Toast. LENGTH_LONG ). show () ;}}) ;}/ ** get the current date and time, and Toast the date and time */private void getCurrentDateTime () {// 1. obtain the current Calendar calendar Calendar = Calendar. getInstance (); // 2. method for obtaining time and date: calendar. get (Calendar. YEAR) Toast. makeText (getApplicationContext (), current time: + calendar. get (Calendar. YEAR) + YEAR + calendar. get (Calendar. MONTH) + MONTH + calendar. get (Calendar. DATE) + day + calendar. get (Calendar. HOUR) + HOUR + calendar. get (Calendar. MINUTE) + MINUTE + calendar. get (Calendar. SECOND) + seconds, Toast. LENGTH_LONG ). show ();}}

:

 

 

5. Date selector DatePicker

 

Common date selector attributes:

--Show Calendar: Android: calendarViewShown. Whether to display the CalendarView calendar component;

--Select final: Android: endYear, whether the selector allows the last year;

--Max date: Android: maxDate, set the maximum date of the date selector, in the format of mm/dd/yyyy;

--Minimum date: Android: minDate: set the minimum date of the date selector, in the format of mm/dd/yyyy;

--Select component: Android: spinnerShown. Whether to display the Spinner component;

--Select the first year: Android: startYear, whether to allow the first year;

 

Instance:

XML source code:

 

 
         
      
  
 

 

 

:

 

 

 

Source code: Http://download.csdn.net/detail/han1202012/6856737

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.