简体   繁体   English

单击后,如何制作一个可重启CountdownTimer的按钮?

[英]How do I make a Button which restarts a CountdownTimer, when clicked?

I have a function that has a timer that I want to "restart" every time you click the button. 我有一个带有计时器的函数,每当您单击该按钮时,我都希望它“重新启动”。 I tried doing this but when the button is clicked several times it appears that there are several timers still going on when I only want the one. 我尝试执行此操作,但是当多次单击按钮时,似乎只有几个定时器仍在运行。 How do I fix this? 我该如何解决?

So, onClick => Cancel last timer => Start new timer 因此,onClick =>取消上一个计时器=>启动新计时器

public void startService(android.view.View view) {
    final SharedPreferences sP = getSharedPreferences("com.example.safetyNet", MODE_PRIVATE);
    final Button button = findViewById(R.id.button3);
    final Intent transIntent = new Intent(this, CheckPinActivity.class);

    CountDownTimer cdt = new CountDownTimer(sP.getInt("TiT", 10) * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            button.setText(String.valueOf(millisUntilFinished).substring(0,2));
        }

        @Override
        public void onFinish() {
            if(sP.getBoolean("lockedDown", false) == true){
                startActivity(transIntent);
            }
        }
    };

    cdt.cancel();
    cdt.start();

}

The problem is that, everytime you call the method "startService(android.view.View view) {}", a new CountDownTimer is created, so the previous CountDownTimer that you created is not the same reference as this one. 问题在于,每次调用方法“ startService(android.view.View view){}”时,都会创建一个新的CountDownTimer,因此之前创建的CountDownTimer与该引用不同。

To solve that, you are going to have to create the CountDownTimer as a class member for your class: 为了解决这个问题,您将必须创建CountDownTimer作为类的类成员:

public YourClass {

  private CountDownTimer cdt;

  .... (do whatever)....

  public void startService(android.view.View view) {
final SharedPreferences sP = getSharedPreferences("com.example.safetyNet", MODE_PRIVATE);
final Button button = findViewById(R.id.button3);
final Intent transIntent = new Intent(this, CheckPinActivity.class);

if (cdt == null) {
 cdt = new CountDownTimer(sP.getInt("TiT", 10) * 1000, 1000) {
    @Override
    public void onTick(long millisUntilFinished) {
        button.setText(String.valueOf(millisUntilFinished).substring(0,2));
    }

    @Override
    public void onFinish() {
        if(sP.getBoolean("lockedDown", false) == true){
            startActivity(transIntent);
        }
    }
};
} else {
  cdt.cancel();
  }
   cdt.start();
}

Hope that helps you 希望对您有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何制作一个程序,当单击按钮时,我可以继续向值添加 1? - How do I make a program where I can keep adding 1 to a value when a button is clicked? 单击“开始”按钮后如何运行进度栏? - How do I make my progress bar run when the “START” button is clicked? 单击按钮后,如何在指定的时间内显示图像? 日食 - How do I make an Image appear for a specified amount of time when a button is Clicked? In eclipse 在android中单击按钮时如何使光标消失 - how can I make curser disappear when button is clicked in android 如何在另一个CountdownTimer中嵌套一个CountdownTimer? - How Do I Have A CountdownTimer Nested Within Another CountdownTimer? 单击按钮时,如何通过编程方式创建一个新的TextView? - How do I create a New TextView Progmatically when a button is clicked? 单击JCheckBox时如何显示JPopupMenu? - How do I make a JPopupMenu appear when a JCheckBox is clicked? 如何在 Android Studio 中制作“如果单击按钮,则执行此操作”? - How to make 'if button clicked, do this' in Android Studio? 如何使一个按钮执行一项任务,该任务取决于Java android中单击了哪个先前的按钮? - How to make one button do a task that depends on which previous buttons has been clicked in Java android? Javafx 单击按钮时,第二个列表视图上的文本被替换为新选择的文本,我如何让它只添加它? - Javafx when button clicked, the text on the the second listview is replaced with the new selected one, how do I make it just add it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM