【问题标题】:Count down timer how to implement onfinish method倒计时定时器如何实现onfinish方法
【发布时间】:2018-03-12 08:38:15
【问题描述】:

我有一个倒计时计时器,我想在完成方法或某种代码上实现,以便当计时器停止时,文本视图变为 时间到了 并启动另一个方法(在活动中)。 澄清一下,计时器被赋予了一个从xx:xx格式倒数到零的起始数字。

定时器的类:

public class countdown_timer {
private  long pls;
private long millisInFuture;
private long countDownInterval;
private boolean status;
public countdown_timer(long pMillisInFuture, long pCountDownInterval) {
    this.millisInFuture = pMillisInFuture;
    this.countDownInterval = pCountDownInterval;
    this.pls = pMillisInFuture;

    status = false;
    Initialize();
}

public void Stop() {
    status = false;
}
public  void Reset() {
    millisInFuture = pls;
}

public long getCurrentTime() {
    return millisInFuture;
}

public void Start() {
    status = true;
}
public void Initialize()
{
    final Handler handler = new Handler();
    Log.v("status", "starting");
    final Runnable counter = new Runnable(){

        public void run(){
            long sec = millisInFuture/1000;
            if(status) {
                if(millisInFuture <= 0) {
                    Log.v("status", "done");
                } else {
                    Log.v("status", Long.toString(sec) + " seconds remain");
                    millisInFuture -= countDownInterval;
                    handler.postDelayed(this, countDownInterval);
                }
            } else {
                Log.v("status", Long.toString(sec) + " seconds remain and timer has stopped!");
                handler.postDelayed(this, countDownInterval);
            }
        }
    };

    handler.postDelayed(counter, countDownInterval);
}

定时器使用的活动:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_card_game_2);
    //...find views
    mycounterup = new countdown_timer(startcard, 1000);
    mycounterdown = new countdown_timer(startcard, 1000);
    RefreshTimer();
    mycounterdown.Start();

  public void RefreshTimer()
{
    final Handler handler = new Handler();
    final Runnable counter = new Runnable(){

        public void run(){
            int minutes_up_start = (int) (mycounterup.getCurrentTime() / 1000) / 60;
            int seconds_up_start = (int) (mycounterup.getCurrentTime() / 1000) % 60;
            String time_2_up_start_formatted = String.format(Locale.getDefault(), "%02d:%02d", minutes_up_start, seconds_up_start);
            card_2_up.setText(time_2_up_start_formatted);

            int minutes_down_start = (int) (mycounterdown.getCurrentTime() / 1000) / 60;
            int seconds_down_start = (int) (mycounterdown.getCurrentTime() / 1000) % 60;
            String card_2_down_start_formatted = String.format(Locale.getDefault(), "%02d:%02d", minutes_down_start, seconds_down_start);
            card_2_down.setText(card_2_down_start_formatted);
            handler.postDelayed(this, 100);
        }
    };

    handler.postDelayed(counter, 100);
}

【问题讨论】:

  • 你可以尝试在你的计时器类中扩展CountDownTimer
  • @Cagri Yalcin 你能告诉我我是怎么做到的吗
  • 为什么不使用内置的 CountDownTimer 类?
  • @godot 你是什么意思?
  • @manos 检查我的答案。希望对您有所帮助。

标签: java android android-studio countdowntimer


【解决方案1】:

你可以使用CountDownTimer:

new CountDownTimer(endsIn * 1000, 1000) { 
public void onTick(long millisUntilFinished) { 
           timerTextView.setText(String.valueOf(millisUntilFinished/1000);
 } 
public void onFinish() {
} 
}.start();

或:

扩展CountDownTimer类:

public class countdown_timer extends CountDownTimer {
    TextView textView;
    @Override
    public void onTick(long millisInFuture) {
        long sec = millisInFuture/1000;
            if(millisInFuture <= 0) {
                Log.v("status", "done");

        } else {
            Log.v("status", Long.toString(sec) + " seconds remain and timer has stopped!");
        }
    }

    @Override
    public void onFinish() {
        if(textView != null){
            // change text in your textview
        }
    }



    public countdown_timer(long pMillisInFuture, long pCountDownInterval) {
        super(pMillisInFuture, pCountDownInterval);

    }
    public countdown_timer(TextView textView, long pMillisInFuture, long pCountDownInterval) {
        super(pMillisInFuture, pCountDownInterval);
        this.textView = textView;




    }






}

这里有两个构造函数,其中一个与您的示例中的相同,在第二个中,您还可以传递 TextView 对象并在 onFinish() 方法中使用它。

更新 2:

这里是活动中的CountDownTimer

public class MainActivity extends AppCompatActivity {

    TextView textView;
    CountDownTimer mycounterdown;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        long startcard = 10000;
        textView = (TextView) findViewById(R.id.test);

        mycounterdown = new CountDownTimer(startcard, 1000) {
            @Override
            public void onTick(long mycounterup) {
                int minutes_up_start = (int) (mycounterup / 1000) / 60;
                int seconds_up_start = (int) (mycounterup / 1000) % 60;
                String time_2_up_start_formatted = String.format(Locale.getDefault(), "%02d:%02d", minutes_up_start, seconds_up_start);
                textView.setText(time_2_up_start_formatted);
            }

            @Override
            public void onFinish() {
                // call here other methods from activity

                testMethod();
            }
        };
        mycounterdown.start();

    }
    public void testMethod(){
        Toast.makeText(MainActivity.this, "Test Method called", Toast.LENGTH_SHORT).show();
    }

}

更新 3:如果最后一个刻度是 1,则不为零将倒计时间隔更改为 500 而不是 1000:

public class MainActivity extends AppCompatActivity {

    TextView textView;
    CountDownTimer mycounterdown;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        long startcard = 10000;
        textView = (TextView) findViewById(R.id.test);

        mycounterdown = new CountDownTimer(startcard, 500) {
            @Override
            public void onTick(long mycounterup) {
                int minutes_up_start = (int) (mycounterup / 1000) / 60;
                int seconds_up_start = (int) (mycounterup / 1000) % 60;
                String time_2_up_start_formatted = String.format(Locale.getDefault(), "%02d:%02d", minutes_up_start, seconds_up_start);
                textView.setText(time_2_up_start_formatted);
            }

            @Override
            public void onFinish() {
                // call here other methods from activity

                testMethod();
            }
        };
        mycounterdown.start();

    }
    public void testMethod(){
        Toast.makeText(MainActivity.this, "Test Method called", Toast.LENGTH_SHORT).show();
    }

}

注意:看看这个answer

【讨论】:

  • 问题是传统的倒数计时器有很多错误,比如跳过最后一个滴答声,这就是我为它创建一个类的原因
  • 跳过最后一个刻度是什么意思?
  • 如果你的意思是最后一次滴答时间为零,那么你可以写 if(millisUntilFinished/1000!=0){ //do stuff }
  • 感谢您的帮助,但我不想添加一个新的 CountDownTimer,我想使用我在计时器类中的一个,但使用完成方法
  • 定时器停止时如何启动另一种方法?
【解决方案2】:

首先,在你的计时器类中扩展CountDownTimer

public class countdown_timer extends CountDownTimer {

}

这允许您实现一些方法。

@Override
public void onTick(long l) {

}

@Override
public void onFinish() {

}

您还必须实现与超类匹配的构造函数。您还可以添加一些附加参数。例如TextView

TextView textView;
public countdown_timer(long millisInFuture, long countDownInterval, TextView txt) {
    super(millisInFuture, countDownInterval);
    textView = txt;
}

onFinish() 是您想要的。还要确保您将此类用作CountDownTimer。然后你就可以启动你的计时器了。 希望对您有所帮助。

【讨论】:

  • 当我将类扩展到 CountDownTimer 时,它显示错误:public countdown_timer(long pMillisInFuture, long pCountDownInterval) "没有可用的默认构造函数"
  • 您必须实现与您的超类匹配的构造函数。编辑我的答案
  • 到目前为止一切顺利,但完成方法在计时器类中,而不是在我的活动中,所以我不能用它来更改文本
  • 您可以更改构造方法以发送您的Textview。检查我编辑的答案
  • 定时器停止时如何启动另一种方法?
猜你喜欢
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多