【发布时间】:2013-04-11 06:18:15
【问题描述】:
我正在使用 Android 上的 CountDownTimer,我陷入了两难境地。在我的代码中,我有以下内容:
public class mCountDownTimer extends CountDownTimer{
protected boolean hasFinished = false;
public mCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}
public void onFinish(){
hasFinished = true;
}
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
}
}
基本上我想知道我的 CountDownTimer 是否已经完成。但是在我想调用它的函数中,我有一些代码:
public void function(){
public boolean finished = false;
if(interrupted)
countDownTimer.cancel();
if(temporaryCountHolder == false){
countDownTimer.start();
interrupted = true;
}
}
如何判断我的计时器是否已结束?我想实现以下内容:
if(countDownTimer.hasFinished == true){
Time now = new Time(); //finds the current time
now.setToNow();
String lsNow = now.format("%m-%d-%Y %I:%M:%S");
lsNow += " just Started\n";
try {
dumpToFile("StepsChanged", lsNow);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
但是如果我把声明放在
之后if(temporaryCountHolder == false)
语句,则带有 hasFinished 的 if 语句将始终评估为假。我怎样才能得到它,以便当且仅当计时器完成时我才能记录时间?
【问题讨论】:
-
你能说明你在
if(temporaryCountHolder == false)之后是如何使用if(countDownTimer.hasFinished == true)的吗? -
@JJPA 我刚刚在帖子中添加了我如何使用 countDownTimer.hasFinished == true 方法
-
是在
if(temporaryCountHolder == false){ countDownTimer.start();interrupted = true;}之后还是在这个语句中? -
@JJPA 是的。我注意到,如果我在之后放置 if 语句,countDownTimer 将不会完成,因此,hasFinished 将是错误的,它永远不会进入那个 if 语句
-
您确定这些语句会在倒计时结束后执行吗?
标签: android if-statement countdown