3.28Android学习

一、今日学习

Android实现计时器功能

计时器工具类

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
  
import androidx.annotation.NonNull;
  
import java.util.Timer;
import java.util.TimerTask;
  
/**
 * 用于计时,在主线程中使用此方法
 */
public class ChjTimer {
  
    private int time;//设置倒计时 X 秒
    private int interval = 1000;//设置间隔时间
    private ChjTimerInter chjTimerInter; //回调
    private Timer timer; // 定时器
    private static final int WHAT_REFREH = 0;//刷新
  
    /**
     * 创建对象则开始计时
     *
     * @param chjTimerInter 接口回调
     */
    public ChjTimer(ChjTimerInter chjTimerInter) {
        this.chjTimerInter = chjTimerInter;
    }
  
    /**
     * 创建对象开始计时
     * @param interval      间隔时间通知(使用第一个方法,默认1秒钟刷新一次)
     * @param chjTimerInter 接口回调
     */
    public ChjTimer(int interval, ChjTimerInter chjTimerInter) {
        this.chjTimerInter = chjTimerInter;
        this.interval = interval;
    }
  
    /**
     * 开始计时
     */
    public void start(int time) {
        this.time = time;
        if (timer == null){
            timer = new Timer();
        } else {
            stop();
            return;
        }
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                timesss.sendMessage(new Message());
            }
        }, interval);
    }
  
    /**
     * 终止计时
     */
    public void stop() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (timesss != null) timesss.removeMessages(WHAT_REFREH);
        if (chjTimerInter != null)chjTimerInter.stop(time);
    }
  
    @SuppressLint("HandlerLeak")
    private Handler timesss = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what != WHAT_REFREH) return;
            time -= 1;
            if (chjTimerInter != null) chjTimerInter.second(time);
            if (time == 0) {
                if (timer == null) return;
                timer.cancel();
                timer = null;
                if (chjTimerInter != null) chjTimerInter.expire();
            } else if (time > 0) {
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        timesss.sendMessage(new Message());
                    }
                }, interval);
            }
        }
    };
  
    /**
     * 接口
     */
    public interface ChjTimerInter {
  
        /**
         * 间隔时间内回调
         */
        void second(int time);
  
        /**
         * 完成回调
         */
        void expire();
  
        /**
         * 终止计时
         */
        void stop(int time);
  
    }
  
}

使用演示

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
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
  
public class MainActivity extends Activity implements View.OnClickListener, ChjTimer.ChjTimerInter {
  
    private TextView tiems,timnew;
    private ChjTimer chjTimer;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tiems = findViewById(R.id.time);
        timnew = findViewById(R.id.timnew);
        findViewById(R.id.but).setOnClickListener(this);
        findViewById(R.id.buts).setOnClickListener(this);
  
        chjTimer = new ChjTimer(this);
    }
  
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.but:
                tiems.setText("10");
                timnew.setText("正在计时");
                chjTimer.start(10);
                break;
            case R.id.buts:
                chjTimer.stop();
                break;
        }
    }
  
    @Override
    public void second(int time) {
        tiems.setText(time + "");
    }
  
    @Override
    public void expire() {
        timnew.setText("计时完成");
    }
  
    @Override
    public void stop(int time) {
        timnew.setText("计时终止" + time);
    }
}

页面布局

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
<?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:orientation="vertical"
    android:layout_gravity="center"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <TextView
        android:id="@+id/timnew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="计时完成"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
    <TextView
        android:id="@+id/time"
        android:text="0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
  
  
    <Button
        android:id="@+id/but"
        android:text="开始"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
  
    <Button
        android:id="@+id/buts"
        android:text="终止"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
  
</LinearLayout>

演示效果 

二、问题

 暂无,原文

三、明日计划

继续Android学习

posted @ 2021-03-28 22:23  居酿圆子  阅读(33)  评论(0编辑  收藏  举报