Android UI component-textview and its subclass (4) chronomete

Source: Internet
Author: User

Chronometer is a simple timer. You can give it a start time and a scheduled time, or if you don't give it a start time, it will use your time to start the call. By default, it is displayed in the form of "minute: Second" or "H: mm: SS" in the current timer value, or set (string) that can be used) the format of the timer value to any string
1. Important attributes
Android: Format of the defined time, for example: hh: mm: SS
2. Important Methods
Setbase (long base): sets the countdown timer.
Setformat (string format): Set the display time format.
Start (): start timing
Stop (): Stop timing

Setonchronometerticklistener (chronometer. onchronometerticklistener listener): called when the timer changes


Instance:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: gravity = "center_horizontal" Android: orientation = "vertical"> <chronometer Android: Id = "@ + ID/chronometer" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: textcolor = "# ffff0000" Android: textsize = "12pt"/> <button Android: Id = "@ + ID/start" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: onclick = "onclick" Android: text = ""/> <button Android: Id = "@ + ID/Stop" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: onclick = "onclick" Android: text = "stop"/> <button Android: id = "@ + ID/RESET" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: onclick = "onclick" Android: TEXT = "reset"/> </linearlayout>

Package COM. light. study. android; import android. app. activity; import android. OS. bundle; import android. OS. systemclock; import android. view. view; import android. widget. button; import android. widget. chronometer; public class mainactivity extends activity {private button startbtn, stopbtn, resetbtn; private chronometer chronomete; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); chronomete = (chronometer) This. findviewbyid (R. id. chronometer); startbtn = (button) This. findviewbyid (R. id. start); stopbtn = (button) This. findviewbyid (R. id. stop); resetbtn = (button) This. findviewbyid (R. id. reset);} public void onclick (view v) {If (V = startbtn) {// start timing chronomete. start ();} else if (V = stopbtn) {chronomete. stop (); // stop timing} else if (V = resetbtn) {chronomete. setbase (systemclock. elapsedrealtime (); // reset }}}

Result:


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.