Android Basic Control Learning-----Date&time

Source: Internet
Author: User

The Date&time here explains the following 6 altogether:

Textclock (text clock), AnalogClock (analog clock), chronometer (timer), DatePicker (date selector), Timepicker (Time picker), CalendarView ( Date view) These six front three are seldom used, the next three if in the actual application may need us to customize, these few control personal feel to use not much, can hand see run on the line.

One Textclock (text clock) is a new control introduced after android4.2 (API17) to replace a previous digital control, textclock the format of the available string to display the current time and date, for the time this control provides two formats, 12-hour and 24-hour system. Here are a few ways to use this:

Is24hourmodeenabled (): Check if the system is using 24-hour display time

Android:format24hour: Set 24-hour format

Android:format12hour: Set 12-hour format

Android:timezone: Setting the time zone

Use:

< Textclock         Android:layout_width = "Match_parent"         android:layout_height= "40DP"        android:format24hour= "yyyy:mm:D d" />

Second, AnalogClock (analog clock)
Summarize several properties.

Android:dial: Background picture of the table

Android:hand_hour: Picture of the hour hand of the table

Android:hand_minute: Picture of the minute of the table

You can try this on your own. Three properties

Three, chronometer (timer)

Just look at an example.

Code:

<?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">    <chronometerAndroid:id= "@+id/chronometer"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:gravity= "Center"Android:textcolor= "#ff0000"android:textsize= "60dip" />    <LinearLayoutAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_margin= "10dip"android:orientation= "Horizontal">        <ButtonAndroid:id= "@+id/btnstart"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Start on record" />        <ButtonAndroid:id= "@+id/btnstop"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Stop on record" />        <ButtonAndroid:id= "@+id/btnreset"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Reset" />        <ButtonAndroid:id= "@+id/btn_format"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Formatted" />    </LinearLayout></LinearLayout>

Java code

 PackageCom.example.test3;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.SystemClock;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.Chronometer;ImportAndroid.widget.Toast; Public classMainactivityextendsActivityImplementsview.onclicklistener,chronometer.onchronometerticklistener{Privatechronometer chronometer; PrivateButton Btn_start,btn_stop,btn_base,btn_format; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    Initview (); }    Private voidInitview () {chronometer=(chronometer) Findviewbyid (r.id.chronometer); Btn_start=(Button) Findviewbyid (R.id.btnstart); Btn_stop=(Button) Findviewbyid (r.id.btnstop); Btn_base=(Button) Findviewbyid (R.id.btnreset); Btn_format=(Button) Findviewbyid (R.id.btn_format); Chronometer.setonchronometerticklistener ( This); Btn_start.setonclicklistener ( This); Btn_stop.setonclicklistener ( This); Btn_base.setonclicklistener ( This); Btn_format.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.btnstart:chronometer.start ();//Start Timing                 Break;  Caser.id.btnstop:chronometer.stop ();//Stop Timing                 Break;  Caser.id.btnreset:chronometer.setbase (Systemclock.elapsedrealtime ());//Reset                 Break;  CaseR.id.btn_format:chronometer.setformat ("time:%s");//change the time display format                 Break; }} @Override Public voidOnchronometertick (chronometer chronometer) {String time=Chronometer.gettext (). toString (); if(Time.equals ("00:00") {toast.maketext (mainactivity). This, "Time is up ~", Toast.length_short). Show (); }    }}

Iv. DatePicker (date selector)

Core properties: As far as the above picture is concerned, it is interesting to try it yourself.

Android:calendartextcolor: The color of the calendar list

Android:calendarviewshown: Whether to display the Calendar view, which is the right piece

Android:datepickermode: component appearance, optional value Spinner,calendar, you can try the effect on your own

Android:dayofweekbackground: Background color of the top of the week

Android:dayofweektextappearance: The text color of the top of the week

Android:endyear last year

Android:firstdayofweek set the Calendar list to start with the day of the week

Android:headerbackground: Background color of the entire head

Android:headerdayofmonthtextappearance: The color of the head date font android:headermonthtextappearance: The font color of the head month

Android:headeryeartextappearance: The font color of the head year

Android:maxdate: Maximum date displayed in this calendar view mm/dd/yyyy format

Android:maxdate: Minimum date displayed in this calendar view mm/dd/yyyy format Android:spinnersshown: Show spinner

Android:startyear: Set the first year (content), such as 19,940 years Android:yearlistitemtextappearance: The text of the list appears in the list. Android:yearlistselectorcolor: Color of the Year list selection

Monitoring Event Ondatechangedlistener

V. Timepicker (Time Picker)

The property is similar to many of the previous, even if the listener event Ontimechangedlistener

Vi. Calendar (calendars view)

Calendar Learning J2SE When there is an important class calendar, almost, with the seemingly use of not much, you can try these several controls, nothing summed up

Android:firstdayofweek: Set the first day of the one week.

Android:maxdate: The maximum date displayed in this calendar view mm/dd/yyyy format android:mindate: Minimum date displayed in this calendar view mm/dd/yyyy format android: Weekdaytextappearance: The weekday text appears in the calendar title abbreviation

The corresponding date Change event is: Calendarview.ondatechangelistener

Summary: Android UI basic components are almost there, as well as some UI components related to adapter adapter next time.

Android Basic Control Learning-----Date&time

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.