如何设置倒计时时间格式

How to set Countdown time format

我使用 runnable() 在我的游戏中制作倒数计时器

我被设置了int cd=30;

这是运行的方法

final Handler mHandler = new Handler();

final Runnable mUpdateTimeTasks = new Runnable() {

    public void run() {
        countdowntext.setText(String.valueOf(cd));
        cd -=1;
        if(cd < 10) {
            countdowntext.setTextColor(Color.RED);
        }
        if(cd == 0) {
           focus.stop();                                      
        }
    };
    // run this in a method.
    mHandler.postDelayed(mUpdateTimeTasks, cd);
}

代码工作正常,但问题是倒数计时器 运行 太快了,我只是想知道如何让 int cd=1 = 1 秒,所以 cd=30 = 30 秒

谁能帮帮我?

i just wonder how to make int cd=1 = 1 second too, so cd=30 = 30 second

那么你应该使用 mHandler.postDelayed(mUpdateTimeTasks, 1000)

final Runnable mUpdateTimeTasks = new Runnable() {
      public void run() {
           countdowntext.setText(String.valueOf(cd));
           cd -=1;
           if(cd < 10) {
                countdowntext.setTextColor(Color.RED);
           }
           if(cd == 0) { 
                focus.stop();        
                mHandler.removaCallbacks(null);
                return;                   
           }
           mHandler.postDelayed(this, 1000)
};

这不会给出准确的 30 秒,但非常接近它

In mHandler.postDelayed(mUpdateTimeTasks, cd); cd is millisecond

您应该更改您的代码:

final Handler mHandler = new Handler();

final Runnable mUpdateTimeTasks = new Runnable() {

public void run() {
    countdowntext.setText(String.valueOf(cd));
    cd -=1;
    if(cd < 10) {
        countdowntext.setTextColor(Color.RED);
    }
    if(cd == 0) {
       focus.stop();                                      
    }
};
// run this in a method.
mHandler.postDelayed(mUpdateTimeTasks, cd);
 }

为此:

    final Handler mHandler = new Handler();

final Runnable mUpdateTimeTasks = new Runnable() {

    public void run() {
        countdowntext.setText(String.valueOf(cd));
        cd -=1;
        if(cd < 10) {
            countdowntext.setTextColor(Color.RED);
        }
        if(cd == 0) {
           focus.stop();    
           mHandler.removaCallbacks(null);  
           return();                                
        }
    };
    // run this in a method.
    mHandler.postDelayed(mUpdateTimeTasks, 1000);
    }

1000 表示在一秒(1000/1000 秒)后检查 Runnable mUpdateTimeTasks 方法。