通过postDelayed方法在烤面包中显示倒计时?

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

使用此方法,我可以设置要执行的动作的延迟:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    //Do something after 5 seconds
  }
}, 5000);

是否有办法在烤面包片中显示这5秒的倒计时?谢谢

java android handler countdown postdelayed
2个回答
3
投票

如果您是说它们堆积造成延迟,那么您应该在显示新面包之前取消之前的面包。

如果您想要更精美的东西,可以尝试使用PopupWindow来显示倒数,那里您有更大的布局自由度,等等。

http://developer.android.com/reference/android/widget/PopupWindow.html

这是其他人的答案:https://stackoverflow.com/a/15174370/2767703


示例:

Toast toast = Toast.makeText(this, "10", Toast.LENGTH_SHORT);
toast.show();
new CountDownTimer(10000, 1000) {  
        public void onTick(long m) {  
           long sec = m/1000+1;  
           toast.cancel();
           toast.setText("" + sec);
           toast.show();
        }  
        public void onFinish() {  
           // Finished  
        }  
    }.start();

1
投票

是,请遵循

new CountDownTimer(30000, 1000) {

    public void onTick(long millisUntilFinished) {
      Log.i("seconds remaining: " ,""+ millisUntilFinished / 1000);
    }

    public void onFinish() {
         Log.i("Timer Finished");
    }  
}.start();
© www.soinside.com 2019 - 2024. All rights reserved.