Timers chronometer and Clocks (AnalogClock and DigitalClock)

Source: Internet
Author: User

Timers chronometer and Clocks (AnalogClock and DigitalClock)

(1) Android provides two clock components: AnalogClock and Digitalclock,digitalclock inherit the TextView component, which means that it is a text box, but the content is always the current time, Therefore, setting the Android:text property for DigitalClock does not work; AnalogClock inherits the view component and overrides the view's OnDraw method, which draws the analog clock on the view.

(2) Similarities and differences between AnalogClock and DigitalClock: They all show the current time. DigitalClock Displays the digital clock, showing the number of seconds in the current time, analogclock the analog clock, and does not display the number of seconds in the current time.

(3) Android also provides a Timer component: Chronometer, the component and the DigitalClock are inherited from TextView, so they all display a piece of text, but Chronometer does not show the current time, It shows how much time has elapsed since the beginning of a certain starting time.

-------Case:

(1) Main.xml file

<?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:gravity= "Center_horizontal"android:orientation= "vertical" >        <!--Defining analog Clocks -    <AnalogClockAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"        />        <!--defining a digital clock -    <DigitalClockAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:textsize= "14pt"Android:textcolor= "#f0f"Android:drawableright= "@drawable/ic_launcher"        />        <!--The timer inherits from the TextView component and does not display the current time, showing how long it has been since a certain starting time -    <chronometerAndroid:id= "@+id/meter"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"        />        <!--Click the button to start the timer -    <ButtonAndroid:id= "@+id/but_start"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Start"/></LinearLayout>

(2) Mainactivity.java file

 PackageCom.yby.clock;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.SystemClock;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.Chronometer;ImportAndroid.widget.Chronometer.OnChronometerTickListener;ImportCOM.YBY.CLOCK.R;/*** Clock AnalogClock and DigitalClock *@authorYinbenyang * DigitalClock inherits from TextView, always displays the current time, the digital clock, can display the current number of seconds * AnalogClock inherits from the view component, overrides the view's OnDraw method, draws the analog clock, The current number of seconds is not displayed*/ Public classMainactivityextendsactivity{PrivateButton But_start; Privatechronometer Meter; /*** Case: Rolex on the phone * Systemclock is a tool class for acquiring system time*/@Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub        Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); But_start=(Button) Findviewbyid (R.id.but_start); Meter=(chronometer) Findviewbyid (r.id.meter); But_start.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub//set the time to start timingmeter.setbase (Systemclock.elapsedrealtime ()); //Start TimerMeter.start (); //set the button to DisabledBut_start.setenabled (false);                }        }); //bind a time listener for chronometerMeter.setonchronometerticklistener (NewOnchronometerticklistener () {@Override Public voidOnchronometertick (chronometer chronometer) {//TODO auto-generated Method Stub//timed over 20s                if(Systemclock.elapsedrealtime ()-meter.getbase () >20*1000){                    //Stop TimerMeter.stop (); //Set button to enableBut_start.setenabled (true);            }            }        }); }}

(3):

Timers chronometer and Clocks (AnalogClock and DigitalClock)

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.