1

使用这种方法,我可以设置我想做的动作的延迟:

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

有没有办法在吐司中显示这 5 秒的倒计时?谢谢

4

2 回答 2

4

如果您的意思是它们堆积导致延迟,那么您应该在展示新的之前取消之前的祝酒词。

如果你想要更花哨的东西,你可以尝试使用 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();
于 2013-12-02T13:54:07.840 回答
1

是 执行以下操作

new CountDownTimer(30000, 1000) {

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

    public void onFinish() {
         Log.i("Timer Finished");
    }  
}.start();
于 2013-12-02T14:11:45.267 回答