本文实例讲述了Android开发实现的计时器功能。分享给大家供大家参考,具体如下:
效果图:
布局:
三个按钮 加上一个Chronometer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res-auto" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" android:orientation = "vertical" android:gravity = "center_horizontal" > < Chronometer android:id = "@+id/test" android:textSize = "25pt" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> < LinearLayout android:layout_width = "match_parent" android:layout_height = "wrap_content" android:orientation = "horizontal" > < Button android:id = "@+id/start" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "开始" android:layout_weight = "1" /> < Button android:id = "@+id/pause" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "暂停" android:layout_weight = "1" /> < Button android:id = "@+id/go_on" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "继续" android:layout_weight = "1" /> </ LinearLayout > </ LinearLayout > |
实现:
四个监听事件 三个按钮 一个计时器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
package com.example.a30797.androidtest; import android.os.SystemClock; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Chronometer; public class MainActivity extends AppCompatActivity { Chronometer ch ; Button start ; Button pause ; Button restart ; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取计时器组件 ch = (Chronometer) findViewById(R.id.test); //获取开始按钮 start = (Button) findViewById(R.id.start) ; //暂停计时按钮 pause = (Button) findViewById(R.id.pause); //继续计时按钮 restart = (Button) findViewById(R.id.go_on); start.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { //设置开始计时时间 ch.setBase(SystemClock.elapsedRealtime() ); //启动计时器 ch.start(); pause.setEnabled( true ); restart.setEnabled( false ); start.setEnabled( false ); } }); //暂停按钮监听器 pause.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { start.setText( "重新开始" ); ch.stop(); start.setEnabled( true ); restart.setEnabled( true ); pause.setEnabled( false ); } }); //暂停按钮监听器 restart.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { start.setText( "重新开始" ); ch.start(); start.setEnabled( true ); pause.setEnabled( true ); restart.setEnabled( false ); } }); //为Chronomter绑定事件监听器 ch.setOnChronometerTickListener( new Chronometer.OnChronometerTickListener() { @Override public void onChronometerTick(Chronometer chronometer) { //如果计时到现在超过了一小时秒 if ( SystemClock.elapsedRealtime() - ch.getBase() > 3600 * 1000 ) { ch.stop(); start.setEnabled( true ); restart.setEnabled( false ); pause.setEnabled( false ); } } }); } } |
希望本文所述对大家Android程序设计有所帮助。
原文链接:https://blog.csdn.net/qq_43377749/article/details/84068373