android带暂停的倒计时demo

本文介绍了一款基于Android平台的番茄工作法时间管理应用开发过程。该应用使用Handler实现了一个可暂停、继续及取消的倒计时功能,帮助用户更灵活地管理专注时间。

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

需要做一个西红柿时间管理应用

因为android自带的CountDownTimer类无法暂停,就用handler先写了个倒计时

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView textView;
    private Button mBtStart,mBtCancel,mBtcontinue,mBtPause;
    private long SysTime;
    private boolean first=true;
    private long m=60000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.TextView_time);
        mBtStart = (Button) findViewById(R.id.Button_start);
        mBtCancel = (Button) findViewById(R.id.Button_cancel);
        mBtcontinue = (Button) findViewById(R.id.Button_continue);
        mBtPause = (Button) findViewById(R.id.Button_pause);

        setTime(m);
    }



    public void start(View view) {
        SysTime=System.currentTimeMillis();
        handler.postDelayed(runnable, 0);
        first=true;
    }

    public void cancel(View view) {
        handler.removeCallbacks(runnable);
        m=60000;
        setTime(m);
    }

    public void pause(View view) {
        handler.removeCallbacks(runnable);
        long t=System.currentTimeMillis()-SysTime;
        m=m-t;
    }

    public void continue1(View view) {
        SysTime=System.currentTimeMillis();
        handler.postDelayed(runnable, 0);
        first=true;
    }

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            long t=System.currentTimeMillis()-SysTime;
            setTime(m-t);
            if(first){
                first=false;
            }
            handler.postDelayed(this, 1000);


        }
    };

    private void setTime(long temp){
        if(temp<=0){
            handler.removeCallbacks(runnable);
            temp=0;
        }

        long time = temp /1000;
        textView.setText(String.format("倒计时  %02d:%02d", time / 60, time % 60));
    }

}

activity_main.xml

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

    <TextView
        android:id="@+id/TextView_time"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/Button_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="start"
            android:text="开始" />

        <Button
            android:id="@+id/Button_pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="pause"
            android:text="暂停" />

        <Button
            android:id="@+id/Button_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="cancel"
            android:text="放弃" />

        <Button
            android:id="@+id/Button_continue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="continue1"
            android:text="继续" />

    </LinearLayout>
</LinearLayout>

示例图




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值