Android study note 13: Timer

Source: Internet
Author: User

Timer Program, hitting start, timer starts timing, stops automatically after 20 seconds, uses the timer control

Package Org. crazyit. time; import android. app. activity; import android. OS. bundle; import android. OS. systemclock; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. chronometer; import android. widget. chronometer. onchronometerticklistener; public class chronometertest extends activity {@ overridepublic void oncreate (bundle savedinstancestate) {s Uper. oncreate (savedinstancestate); setcontentview (R. layout. main); // obtain the timer component final chronometer CH = (chronometer) findviewbyid (R. id. test); // obtain the start button start = (button) findviewbyid (R. id. start); start. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (View Source) {// set the start time ch. setbase (systemclock. elapsedrealtime (); // start the timer ch. start () ;}}); // timer listener ch. setonchronometerticklistener (New on Chronometerticklistener () {@ overridepublic void onchronometertick (chronometer ch) {// If the timer time has exceeded 20 s from the start. If (systemclock. elapsedrealtime ()-ch. getbase ()> 20*1000) {Ch. Stop ();}}});}}

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center_horizontal">
<! -- Timer control --> <chronometerandroid: Id = "@ + ID/test" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: textsize = "12pt" Android: textcolor = "# ffff0000"/> <buttonandroid: Id = "@ + ID/start" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "start"/> </linearlayout>
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.