可能我们在开发中会时常用到计时器这玩意儿,比如在录像的时候,我们可能需要在右上角显示一个计时器。这个东西其实实现起来非常简单。
只需要用一个控件Chronometer,是的,就这么简单,我都不好意思讲述一下了。
1 <Chronometer
2 android:layout_width="wrap_content"
3 android:layout_height="wrap_content"
4 android:format="%s"
5 android:id="@+id/timer"/>
是的,就这么简单。java代码同样
1 @Override
2 protected void onCreate(Bundle savedInstanceState) {
3 super.onCreate(savedInstanceState);
4 setContentView(R.layout.activity_main);
5 timer = (Chronometer) findViewById(R.id.timer);
6 }
7
8 public void btnClick(View view) {
9 timer.setBase(SystemClock.elapsedRealtime());//计时器清零
10 timer.start();
11 }
超简单有木有?看看运行结果:
或许你会说,这个要是需要显示上时间怎么弄呢?不急不急,两行代码就能解决的事情。
1 public void btnClick(View view) {
2 timer.setBase(SystemClock.elapsedRealtime());//计时器清零
3 int hour = (int) ((SystemClock.elapsedRealtime() - timer.getBase()) / 1000 / 60);
4 timer.setFormat("0"+String.valueOf(hour)+":%s");
5 timer.start();
6 }
7
8 public void stopClick(View view) {
9 timer.stop();
10 }
恩,对,就是 这么简单,不过别忘了把xml的format改一下
1 <Chronometer
2 android:layout_width="match_parent"
3 android:layout_height="wrap_content"
4 android:format="00:00:00"
5 android:gravity="center"
6 android:id="@+id/timer"/>
是的,你没有看错,这样就可以了,不信,你看!
就和你想象的录像上方的时间一样有木有?恩。你前面设置一个圆圈,再设置计时器颜色就和它一样有逼格了。
而或许你并不喜欢用这种方式,当然用handler+timer+timerTask的方式也是可以的啦。由于太简单,就直接上代码了。
JAVA逻辑代码:
1 package com.example.nanchen.timerdemo;
2
3 import android.os.SystemClock;
4 import android.support.annotation.Nullable;
5 import android.support.v7.app.AppCompatActivity;
6 import android.os.Bundle;
7 import android.view.View;
8 import android.widget.Chronometer;
9 import android.widget.TextView;
10
11 import java.util.Locale;
12 import java.util.Timer;
13 import java.util.TimerTask;
14
15 public class MainActivity extends AppCompatActivity {
16
17 private Chronometer timer;
18 private Timer timer1;
19 private TextView textView;
20 private TimerTask timerTask;
21
22
23 @Override
24 protected void onCreate(@Nullable Bundle savedInstanceState) {
25 super.onCreate(savedInstanceState);
26 setContentView(R.layout.activity_main);
27 timer = (Chronometer) findViewById(R.id.timer);
28
29 textView = (TextView) findViewById(R.id.text);
30 timer1 = new Timer();
31 }
32
33 public void btnClick(View view) {
34 timer.setBase(SystemClock.elapsedRealtime());//计时器清零
35 int hour = (int) ((SystemClock.elapsedRealtime() - timer.getBase()) / 1000 / 60);
36 timer.setFormat("0"+String.valueOf(hour)+":%s");
37 timer.start();
38 }
39
40 public void stopClick(View view) {
41 timer.stop();
42 }
43
44 public void startClick(View view) {
45 timerTask = new TimerTask() {
46 int cnt = 0;
47 @Override
48 public void run() {
49 runOnUiThread(new Runnable() {
50 @Override
51 public void run() {
52 textView.setText(getStringTime(cnt++));
53 }
54 });
55 }
56 };
57 timer1.schedule(timerTask,0,1000);
58 }
59
60 private String getStringTime(int cnt) {
61 int hour = cnt/3600;
62 int min = cnt % 3600 / 60;
63 int second = cnt % 60;
64 return String.format(Locale.CHINA,"%02d:%02d:%02d",hour,min,second);
65 }
66
67 public void stopClick1(View view) {
68 if (!timerTask.cancel()){
69 timerTask.cancel();
70 timer1.cancel();
71 }
72 }
73 }
XML文件:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 xmlns:tools="http://schemas.android.com/tools"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 android:orientation="vertical"
8 tools:context="com.example.nanchen.timerdemo.MainActivity">
9
10 <Chronometer
11 android:layout_width="match_parent"
12 android:layout_height="wrap_content"
13 android:format="00:00:00"
14 android:gravity="center"
15 android:id="@+id/timer"/>
16 <Button
17 android:layout_width="match_parent"
18 android:onClick="btnClick"
19 android:text="start"
20 android:layout_height="wrap_content"/>
21 <Button
22 android:layout_width="match_parent"
23 android:text="stop"
24 android:onClick="stopClick"
25 android:layout_height="wrap_content"/>
26 <View
27 android:layout_width="match_parent"
28 android:layout_height="1dp"
29 android:background="#959393"
30 android:layout_marginBottom="20dp"
31 android:layout_marginTop="20dp"/>
32 <TextView
33 android:layout_width="match_parent"
34 android:layout_height="wrap_content"
35 android:text="00:00:00"
36 android:gravity="center"
37 android:id="@+id/text"/>
38 <Button
39 android:layout_width="match_parent"
40 android:layout_height="wrap_content"
41 android:text="开始"
42 android:onClick="startClick"/>
43 <Button
44 android:layout_width="match_parent"
45 android:layout_height="wrap_content"
46 android:text="停止"
47 android:onClick="stopClick1"/>
48
49
50 </LinearLayout>
简单运行下方用timer实现的效果:
想必大家到这样都会有了自己的理解,android 官方的Chronometer方式只是为了做一个计时器,而我们采用自己用Timer和TimerTask方式可以更加自主,因为你可以想从什么时间开始计时就从什么时间开始计时,计时方式想顺计时倒计时都不是难事儿,甚至各种浮夸的隔两秒,隔三秒,隔n秒都是可以的,具体使用就看你选择咯~~
转载的小伙伴别忘了附上本文原创链接哦,嘿嘿,谢谢配合:android 两种实现计时器时分秒的实现,把时间放在你的手中~ - 南尘 - 博客园