Android简易倒计时

魔法使者 2024-05-19 ⋅ 39 阅读

倒计时功能在许多Android应用程序中都十分普遍。它可以用于各种场景,例如倒计时游戏、定时提醒和倒计时计划等。在本篇博客中,我们将介绍如何构建一个简单而实用的倒计时应用程序。

准备工作

在开始之前,我们需要创建一个新的Android项目。打开Android Studio,点击"Start a new Android Studio project",然后按照向导指引完成项目的创建。

创建倒计时界面

首先,我们需要创建一个包含倒计时功能的主界面。在activity_main.xml文件中,我们添加一个TextView控件来显示倒计时的剩余时间,和三个Button控件用来开始、暂停和重置倒计时。

<RelativeLayout 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:paddingLeft="16dp"
    android:paddingTop="16dp"
    android:paddingRight="16dp"
    android:paddingBottom="16dp"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tvCountdown"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="48sp"
        android:layout_centerInParent="true"/>

    <Button
        android:id="@+id/btnStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tvCountdown"
        android:text="Start"
        android:layout_marginTop="16dp"/>

    <Button
        android:id="@+id/btnPause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btnStart"
        android:text="Pause"
        android:layout_marginTop="16dp"/>

    <Button
        android:id="@+id/btnReset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btnPause"
        android:text="Reset"
        android:layout_marginTop="16dp"/>

</RelativeLayout>

编写倒计时逻辑

接下来,我们需要编写倒计时的逻辑。在MainActivity.java文件中,我们使用CountDownTimer类来实现倒计时功能。CountDownTimer是Android SDK中提供的一个用于实现倒计时的类,我们只需重写其中的两个方法:onTickonFinish

public class MainActivity extends AppCompatActivity {

    private TextView tvCountdown;
    private Button btnStart, btnPause, btnReset;
    private CountDownTimer countDownTimer;
    private boolean timerRunning;
    private long timeLeft;

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

        tvCountdown = findViewById(R.id.tvCountdown);
        btnStart = findViewById(R.id.btnStart);
        btnPause = findViewById(R.id.btnPause);
        btnReset = findViewById(R.id.btnReset);

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startTimer();
            }
        });

        btnPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pauseTimer();
            }
        });

        btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetTimer();
            }
        });
    }

    private void startTimer() {
        if (!timerRunning) {
            countDownTimer = new CountDownTimer(timeLeft, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    timeLeft = millisUntilFinished;
                    updateCountdownText();
                }

                @Override
                public void onFinish() {
                    // 完成倒计时后的操作
                }
            }.start();

            btnStart.setText("Pause");
            timerRunning = true;
        } else {
            pauseTimer();
        }
    }

    private void pauseTimer() {
        countDownTimer.cancel();
        btnStart.setText("Start");
        timerRunning = false;
    }

    private void resetTimer() {
        timeLeft = 60000; // 设置倒计时时长(以毫秒为单位)
        updateCountdownText();
        btnStart.setText("Start");
        timerRunning = false;
    }

    private void updateCountdownText() {
        int minutes = (int) (timeLeft / 1000) / 60;
        int seconds = (int) (timeLeft / 1000) % 60;

        String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);

        tvCountdown.setText(timeLeftFormatted);
    }
}

测试倒计时功能

现在我们的倒计时应用程序已经准备就绪了。点击运行按钮,将应用程序部署到模拟器或真机上进行测试。

通过点击“Start”按钮,倒计时将开始,并且TextView控件上的剩余时间会随着时间的推移而更新。你可以点击“Pause”来暂停倒计时,点击“Reset”按钮来重置倒计时。

这只是一个简单的倒计时应用程序的实现示例。你可以根据自己的需求,扩展此应用程序并添加更多功能,例如声音提醒、界面动画和设置倒计时时长等。希望这篇博客对你有所帮助,祝你编码愉快!

作者:AI助手 日期:2022年1月20日


全部评论: 0

    我有话说: