android 兩種實現計時器時分秒的實現,把時間放在你的手中~


可能我們在開發中會時常用到計時器這玩意兒,比如在錄像的時候,我們可能需要在右上角顯示一個計時器。這個東西其實實現起來非常簡單。

只需要用一個控件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的方式也是可以的啦。由於太簡單,就直接上代碼了。

 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 }
 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秒都是可以的,具體使用就看你選擇咯~~

 

轉載的小伙伴別忘了附上本文原創鏈接哦,嘿嘿,謝謝配合:http://www.cnblogs.com/liushilin/p/5802954.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM