【问题标题】:Android count down timer explanation wantedAndroid倒数计时器解释通缉
【发布时间】:2014-09-09 18:00:28
【问题描述】:

在 android 参考资料中,我找到了一个关于倒计时的页面:CountDownTimers

里面有这段代码:

    new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
     }
  }.start();

我想知道它是什么,如果它是一个对象或类,以及我如何将它用作不同类对象的计时器。有人可以解释一下我如何将它放入我的android项目并使用它。谢谢。

【问题讨论】:

    标签: android countdowntimer


    【解决方案1】:

    公共构造函数:

    CountDownTimer(long millisInFuture, long countDownInterval)
    

    公共方法:

    final void  cancel()
        Cancel the countdown.
    
    abstract void   onFinish()
        Callback fired when the time is up.
    
    abstract void   onTick(long millisUntilFinished)
        Callback fired on regular interval.
    
    synchronized final CountDownTimer   start()
        Start the countdown.
    

    公共构造函数:

    public CountDownTimer (long millisInFuture, long countDownInterval)
    Parameters
    1. millisInFuture   The number of millis in the future from the call to start() until the countdown is done and onFinish() is called.
    2. countDownInterval    The interval along the way to receive onTick(long) callbacks.
    

    公共方法:

    public final void cancel ()
        Cancel the countdown.
    
    public abstract void onFinish ()
        Callback fired when the time is up.
    
    public abstract void onTick (long millisUntilFinished)
        Callback fired on regular interval.
            Parameters: millisUntilFinished     The amount of time until finished.
    
    public final synchronized CountDownTimer start ()
        Start the countdown.
    

    教程链接:

    link 1

    link 2

    link 3

    编辑:

    new CountDownTimer(30000 /*For how long should timer run*/, 1000 /*time interval after which `onTick()` should be called*/) {
    
     public void onTick(long millisUntilFinished) {
         Log.i("Countdown Timer: ","seconds remaining: " + millisUntilFinished / 1000);
     }
    
     public void onFinish() {
         //Done timer time out.
         myTextView.setText("Time Out.!");
     }
    }.start();
    

    【讨论】:

    • 谢谢抱歉,如果我对 android 文档中的解释有点盲目。我想问一下,如果我想让 countdowntimer 类的每个对象在 onFinish() 方法中发生自己的事件,我会怎么做。
    • 你能不能快点回答,对不起。
    • 你希望如何使用倒计时
    • 我想从中创建对象,这些对象在 onfinish() 方法中有自己的事物或事件
    【解决方案2】:

    你引用的参考资料解释得很清楚。

    CountDownTimer 是一个CLASS,用于执行确定的任务,直到完成。

    onTick 方法用于对每个刻度(以毫秒为单位)执行一个操作,并且

    public void onTick(long millisUntilFinished) {
         //It will perform this task every millisecond until countdown finishes.
     }
    

    onFinish 方法执行最后一个任务,直到类结束的实例。

     public void onFinish() {
         //It will perform this task only once, when the countdown finishes.
     } 
    

    【讨论】:

    • 谢谢抱歉,如果我对 android 文档中的解释有点盲目。我想问如果我希望 countdowntimer 类的每个对象在 onFinish() 方法中发生自己的事件,我会怎么做。
    【解决方案3】:

    From the docs

    公共抽象类

    倒计时

    扩展对象

    应该回答一个问题。

    至于如何使用,see this answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多