Chronometer android计时器组件Chronometer的使用,android通话时长计时控件

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

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

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

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


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


Chronometer介绍

Class that implements a simple timer.
 * <p>
 * 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开发中的使用。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章