当前位置: 移动技术网 > IT编程>移动开发>Android > Android时分秒计时器的两种实现方法

Android时分秒计时器的两种实现方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

安庆长途汽车站,排骨炖土豆,曾志平

可能我们在开发中会时常用到计时器这玩意儿,比如在录像的时候,我们可能需要在右上角显示一个计时器。这个东西其实实现起来非常简单。

只需要用一个控件chronometer,是的,就这么简单,我都不好意思讲述一下了。

<chronometer
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format="%s"
android:id="@+id/timer"/>

是的,就这么简单。java代码同样

@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
timer = (chronometer) findviewbyid(r.id.timer);
}
public void btnclick(view view) {
timer.setbase(systemclock.elapsedrealtime());//计时器清零
timer.start();
}

超简单有木有?看看运行结果:

或许你会说,这个要是需要显示上时间怎么弄呢?不急不急,两行代码就能解决的事情。

public void btnclick(view view) {
timer.setbase(systemclock.elapsedrealtime());//计时器清零
int hour = (int) ((systemclock.elapsedrealtime() - timer.getbase()) / 1000 / 60);
timer.setformat("0"+string.valueof(hour)+":%s");
timer.start();
}
public void stopclick(view view) {
timer.stop();
}

恩,对,就是 这么简单,不过别忘了把xml的format改一下

<chronometer
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:format="00:00:00"
android:gravity="center"
android:id="@+id/timer"/>

是的,你没有看错,这样就可以了,不信,你看!

就和你想象的录像上方的时间一样有木有?恩。你前面设置一个圆圈,再设置计时器颜色就和它一样有逼格了。

而或许你并不喜欢用这种方式,当然用handler+timer+timertask的方式也是可以的啦。由于太简单,就直接上代码了。

package com.example.nanchen.timerdemo;
import android.os.systemclock;
import android.support.annotation.nullable;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.chronometer;
import android.widget.textview;
import java.util.locale;
import java.util.timer;
import java.util.timertask;
public class mainactivity extends appcompatactivity {
private chronometer timer;
private timer timer1;
private textview textview;
private timertask timertask;
@override
protected void oncreate(@nullable bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
timer = (chronometer) findviewbyid(r.id.timer);
textview = (textview) findviewbyid(r.id.text);
timer1 = new timer();
}
public void btnclick(view view) {
timer.setbase(systemclock.elapsedrealtime());//计时器清零
int hour = (int) ((systemclock.elapsedrealtime() - timer.getbase()) / 1000 / 60);
timer.setformat("0"+string.valueof(hour)+":%s");
timer.start();
}
public void stopclick(view view) {
timer.stop();
}
public void startclick(view view) {
timertask = new timertask() {
int cnt = 0;
@override
public void run() {
runonuithread(new runnable() {
@override
public void run() {
textview.settext(getstringtime(cnt++));
}
});
}
};
timer1.schedule(timertask,0,1000);
}
private string getstringtime(int cnt) {
int hour = cnt/3600;
int min = cnt % 3600 / 60;
int second = cnt % 60;
return string.format(locale.china,"%02d:%02d:%02d",hour,min,second);
}
public void stopclick1(view view) {
if (!timertask.cancel()){
timertask.cancel();
timer1.cancel();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<linearlayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.nanchen.timerdemo.mainactivity">
<chronometer
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:format="00:00:00"
android:gravity="center"
android:id="@+id/timer"/>
<button
android:layout_width="match_parent"
android:onclick="btnclick"
android:text="start"
android:layout_height="wrap_content"/>
<button
android:layout_width="match_parent"
android:text="stop"
android:onclick="stopclick"
android:layout_height="wrap_content"/>
<view
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#959393"
android:layout_marginbottom="20dp"
android:layout_margintop="20dp"/>
<textview
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="00:00:00"
android:gravity="center"
android:id="@+id/text"/>
<button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开始"
android:onclick="startclick"/>
<button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止"
android:onclick="stopclick1"/>
</linearlayout>

简单运行下方用timer实现的效果:

想必大家到这样都会有了自己的理解,android 官方的chronometer方式只是为了做一个计时器,而我们采用自己用timer和timertask方式可以更加自主,因为你可以想从什么时间开始计时就从什么时间开始计时,计时方式想顺计时倒计时都不是难事儿,甚至各种浮夸的隔两秒,隔三秒,隔n秒都是可以的,具体使用就看你选择咯~~

以上所述是小编给大家介绍的android时分秒计时器的两种实现方法,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网