Chronometer是一个简单的定时器,你可以给它一个开始时间,并以此定时,或者如果你不给它一个开始时间,它将会使用你的时间通话开始。默认情况下它会显示在当前定时器的值的形式“分:秒”或“H:MM:SS的”,或者可以使用的Set(字符串)格式的定时器值到一个任意字符串
1.重要属性
android:format:定义时间的格式如:hh:mm:ss
2.重要方法
setBase(long base):设置倒计时定时器
setFormat(String format):设置显示时间的格式。
start():开始计时
stop():停止计时

setOnChronometerTickListener(Chronometer.OnChronometerTickListener listener):当计时器改变时调用

实例:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:gravity="center_horizontal"
  6. android:orientation="vertical" >
  7.  
  8. <Chronometer
  9. android:id="@+id/chronometer"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:textColor="#ffff0000"
  13. android:textSize="12pt" />
  14.  
  15. <Button
  16. android:id="@+id/start"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:onClick="onClick"
  20. android:text="启动" />
  21.  
  22. <Button
  23. android:id="@+id/stop"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:onClick="onClick"
  27. android:text="停止" />
  28.  
  29. <Button
  30. android:id="@+id/reset"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:onClick="onClick"
  34. android:text="复位" />
  35. </LinearLayout>
  1. package com.light.study.android;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.os.SystemClock;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.Chronometer;
  9.  
  10. public class MainActivity extends Activity {
  11. private Button startBtn,stopBtn,resetBtn;
  12. private Chronometer chronomete;
  13. @Override
  14. public void onCreate(Bundle savedInstanceState)
  15. {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. chronomete = (Chronometer) this.findViewById(R.id.chronometer);
  19. startBtn = (Button) this.findViewById(R.id.start);
  20. stopBtn = (Button) this.findViewById(R.id.stop);
  21. resetBtn = (Button) this.findViewById(R.id.reset);
  22. }
  23.  
  24. public void onClick(View v) {
  25. if (v == startBtn) {// 开始计时
  26. chronomete.start();
  27. } else if (v == stopBtn) {
  28. chronomete.stop();// 停止计时
  29. } else if (v == resetBtn) {
  30. chronomete.setBase(SystemClock.elapsedRealtime());// 复位
  31. }
  32. }
  33.  
  34. }

结果:

Android用户界面 UI组件--TextView及其子类(四) Chronometer计时器的更多相关文章

  1. Android用户界面 UI组件--TextView及其子类(二) Button,selector选择器,sharp属性

    1.XML文件中的OnClick 属性可以指定在Activity中处理点击事件的方法,Activity中必须定义该属性指定的值作为方法的名字且有一个View类型的参数,表示此物件被点击. 2.使用se ...

  2. Android用户界面 UI组件--TextView及其子类(三) EditView以及各种Span文字样式讲解

    EditView和TextView的用法差不多,只是文字可编辑 小技巧: 设置EditText隐藏键盘  setInputType(0); 设置EditText不被输入法遮盖  getWindow() ...

  3. Android用户界面 UI组件--TextView及其子类(一) TextView

    1.TextView android:autoLink设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none /web/email/phone/map/a ...

  4. Android用户界面UI组件--AdapterView及其子类(四) GridView

    GridView常用的XML属性: android:columnWidth  设置列的宽度. android:horizontalSpacing  两列之间的间距.  android:numColum ...

  5. Android用户界面 UI组件--TextView及其子类(五) DigitalClock,AnalogClock,RadioButton,CheckBox,ToggleButton汇总

    DigitalClock和AnalogClock两个时钟类 可以为DigitalClock设置背景图片,自定义时针,秒针,分针的样式 例子: <?xml version="1.0&qu ...

  6. Android用户界面 UI组件--AdapterView及其子类(一) ListView及各种Adapter详解

    ListView就是列表组件,一般通过继承ListActivity使用系统提供的ListView. 所有的AdapterView组件都需要有一个对应的Adapter作为适配器来显示列表中元素的布局方式 ...

  7. Android用户界面UI组件--AdapterView及其子类(五) Spinner和SpinnerAdapter

    Spinner就是下拉框组件,可以自定义下拉布局样式,可以使用ArrayAdapter以及SpinnerAdapter适配 在Adapter中实现SpinnerAdapter,继承BaseAdapte ...

  8. Android用户界面UI组件--AdapterView及其子类(三) ExpandableListView

    ExpandableListView: List中的每一项可以展开收缩. 一种伸缩式的ListView. android:cacheColorHint="#00000000" 这个 ...

  9. Android用户界面 UI组件--ImageView及其子类ImageButton,QuickContactBadge附带Draw9Patch工具说明

    1.ImageView 常用属性: android:src 设置可绘制对象作为 ImageView 显示的内容 android:cropToPadding 如果设置为true,图片裁剪到保留该Imag ...

随机推荐

  1. Unity3D 之3D游戏SD快打 3D游戏基础入门开发全(1)

    这里记录一个U3D游戏,3D游戏的基本开发. 导入素材 1.首先导入需要的素材.因为FBX格式的素材是通用的,所以尽量导入这样的资源使用 导入后的结果: 然后对人形骨骼进行设置. 看哪里没有映射到骨骼 ...

  2. jQuery Validation Plugin学习

    http://blog.csdn.net/violet_day/article/details/14109261 jQuery Validation Plugin Demo 一.默认校验规则 (1)r ...

  3. 16Aspx.com源码2013年10月到2013年12月详细

    创建时间FROM: 创建时间TO:   ExtJS合同管理信息系统源码 2013-12-13   [VS2008] 源码介绍: ExtJS合同管理信息系统源码浏览器兼容:IE,Firefox,谷歌等主 ...

  4. 20151212Jquery 工具函数代码备份

    $(function () { /*var str=' jquery '; alert(str); alert($.trim(str));*/ /*var arr=['张三','李四','王五','麻 ...

  5. Service解析

    Service解析: 运行service有如下两种方式: StartService() 访问者退出,service仍然运行: BindService() 访问者与service绑定,访问者退出,ser ...

  6. ACM——进制转换

    http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1012 进制转换 时间限制(普通/Jav ...

  7. Power Map 更新日志

    2015-05-18,五月更新 Custom Regions feature,允许用户自定义区域要素,支持kml和shape格式 New customization features,包括图例/文本框 ...

  8. Codevs 2597 团伙(并查集)

    2597 团伙 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么 ...

  9. leetcode344——Reverse String(C++)

    Write a function that takes a string as input and returns the string reversed. Example:Given s = &qu ...

  10. 【转】Vim命令合集以及乱码问题解决

    乱码问题 """""""""""""""" ...