创建一个在循环之间有一秒间隔的循环

问题描述 投票:0回答:1

我正在尝试创建一个倒数计时器,该计时器将为测验应用程序更新Java中的jlabel。到目前为止,我的代码已经有了这个,但是它给sleep()方法带来了错误,并且无法运行我的程序。

    while (timer > 0) {
     lblTimer.setText(Integer.toString(timer));
     Thread.sleep(1000);
     timer--;

    }
java loops jlabel sleep
1个回答
0
投票

希望对您有帮助,

package com.example.timer;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;

import androidx.annotation.Nullable;

public class Try extends Activity {
    private CountDownTimer countDownTimer;
    private String TAG=Try.class.getSimpleName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new CountDownTimer(30000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                Log.d(TAG, "onTick: secs remaining "+millisUntilFinished/1000);
            }

            @Override
            public void onFinish() {
                Log.d(TAG, "onFinish: doneeee");
            }
        }.start();

    }
}

© www.soinside.com 2019 - 2024. All rights reserved.