在安卓开发过程中经常需要计数器的使用,我们可以用handler实现间隔性的操作。

例如按两次返回back退出,隔几秒重复换页面等。

但是也经常有一类需求类是打电话时候记录时长的操作。

这就用到了android中 android.widget包下的Chronometer 这个组件。


转载请注明:http://blog.csdn.net/xuanguofeng/article/details/45579037    谢谢


Chronometer介绍

Class that implements a simple timer.
 *


 * You can give it a start time in the {@link SystemClock#elapsedRealtime} timebase,
 * and it counts up from that, or if you don't give it a base time, it will use the
 * time at which you call {@link #start}.  By default it will display the current
 * timer value in the form "MM:SS" or "H:MM:SS", or you can use {@link #setFormat}
 * to format the timer value into an arbitrary string.
 *
 * @attr ref android.R.styleable#Chronometer_format



本人英语不好 ,就不在这献丑了。抱歉。。。。。



首先先看布局文件



三个按钮和 一个Chronometer的控件 没有特殊方法



Activity逻辑代码部分


大部分方法都已经注释

注意 chronometer.getText();方法返回的CharSequence这个对象,我们通过这个对象可以操作里面的数据

CharSequence类里面的方法有:

 public int length();

 public char charAt(int index);

 public CharSequence subSequence(int start, int end);

 public String toString();

这四个方法看名字也知道啥意思第一个获取长度、第二根据位置获取字符、第三个截取、第四个toString


我们可以根据获取到的属性进行不同的操作,例如:到5秒的时候进行操作可以

if("00:05".equals(text.toString())){

  要进行的操作;

}


也可以获取字符

if( text.charAt(text.length()-1)==5){

  要进行的操作;

}


还可以自己拆分字符串进行操作总之各种方法就不献丑了。



这就是一个简单计时器在android开发中的使用。




更多相关文章

  1. Android(安卓)onTouchEvent, onClick及onLongClick的调用机制
  2. Android(安卓)如何自定义一个简单的组件和自定义的点击事件(中级)
  3. Android五大基本组件
  4. 进度圈的显示
  5. Android(安卓)Graphics专题(1)--- Canvas基础
  6. [Android] 使用Matrix矩阵类对图像进行缩放、旋转、对比度、亮度
  7. Android(安卓)View measure流程详解
  8. 浅谈Java中Collections.sort对List排序的两种方法
  9. Python list sort方法的具体使用

随机推荐

  1. 如何启动Android(安卓)SDK 1.5模拟器
  2. Android(安卓)Studio 默认keystore 以及
  3. android 获取文件夹、文件的大小 以B、KB
  4. Android的焦点(Focus)问题
  5. android中ActionBar以及其与Action Butto
  6. android开发小知识2
  7. android 支持的声音格式详解
  8. Android的webView和h5+js交互
  9. Android对弹出输入法界面影响app界面布局
  10. 《Android学习指南》文件夹