Timer——倒计时小玩具

本文介绍了一个简单的Android倒计时应用实现方式,通过Timer和TimerTask进行计时,并使用Handler更新UI显示。文章提供了完整的代码示例,展示了如何启动和停止计时器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

往后几天应该会看线程的知识

先看一个  倒计时的小Demo



    private Handler mHandler = new Handler() {
        public void handleMessage(Message mes){
            mTvTime.setText(mes.arg1+"");
            startTime();
        };
    };
    /**
     * 开始计时器
     */
    public void startTime(){
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                if (i<=0){
                    stopTime();
                    Toast.makeText(getApplicationContext(),"倒计时结束",Toast.LENGTH_SHORT).show();
                }else {
                    i--;
                    Message message = mHandler.obtainMessage();
                    message.arg1 = i;
                    mHandler.sendMessage(message);
                }
            }
        };
        timer.schedule(timerTask,1000);
    }

    /**
     * 结束计时器
     */
    public void stopTime(){
        timer.cancel(); //清除线程
    }

这里主要运用的是Timer 

既然用到了Timer 那就少不了TimerTask了

Timer 是 Android 机制中提供的一个计时器


还有一个需要注意的是 只能在ui线程中修改ui 


下面附上源码

<?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">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_setTime"/>
    <Button
        android:id="@+id/btn_getTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获得时间"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv_time"
        android:textSize="18sp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始倒计时"/>

    <Button
        android:id="@+id/btn_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="结束倒计时"/>
    </LinearLayout>
</LinearLayout>



import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button mBtnGetTime;
    private Button mBtnStart;
    private Button mBtnStop;
    private TextView mTvTime;
    private EditText mEtTime;
    private int i=0;
    private Timer timer = null;
    private TimerTask timerTask = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    /**
     * 初始化布局
     */
    private void initView() {
        mBtnGetTime = (Button) findViewById(R.id.btn_getTime);
        mBtnStart = (Button) findViewById(R.id.btn_start);
        mBtnStop = (Button) findViewById(R.id.btn_stop);
        mTvTime = (TextView) findViewById(R.id.tv_time);
        mEtTime = (EditText) findViewById(R.id.et_setTime);

        mBtnGetTime.setOnClickListener(this);
        mBtnStart.setOnClickListener(this);
        mBtnStop.setOnClickListener(this);
    }



    /**
     * 点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_getTime:
                mTvTime.setText(mEtTime.getText());
                i = Integer.parseInt(mTvTime.getText().toString());
                break;
            case R.id.btn_start:
                startTime();
                break;
            case R.id.btn_stop:
                stopTime();
                break;
        }
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message mes){
            mTvTime.setText(mes.arg1+"");
            startTime();
        };
    };


    /**
     * 开始计时器
     */
    public void startTime(){
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                if (i<=0){
                    stopTime();
                    Toast.makeText(getApplicationContext(),"倒计时结束",Toast.LENGTH_SHORT).show();
                }else {
                    i--;
                    Message message = mHandler.obtainMessage();
                    message.arg1 = i;
                    mHandler.sendMessage(message);
                }
            }
        };
        timer.schedule(timerTask,1000);
    }

    /**
     * 结束计时器
     */
    public void stopTime(){
        timer.cancel();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值