我在通过此方法取消此计时器时遇到问题。
public void nezablokovat() {
int secondsToRun = 9999999;
final ValueAnimator timern = ValueAnimator.ofInt(secondsToRun);
timern.setDuration(secondsToRun * 1000).setInterpolator(new LinearInterpolator());
timern.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int elapsedSeconds = (int) animation.getAnimatedValue();
int minutes = elapsedSeconds / 60;
int seconds = elapsedSeconds % 60;
if (seconds%10 == 1) {
Pis("$$$");
}
}
});
timern.start();
}
我想将 timern.cancel();
放入按钮的 onClick
监听器中的其他方法。
请问您有什么想法吗?
如果我将 timern.cancel()
放在这里:
void teplotahore() {
STup.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
timern.cancel();
}
}
}
);
}
编译器不知道timern
。
最佳答案
我不知道你的整个类是如何构造的,但我相信你也许可以将“timern”“变量”移到构造函数之外(在它上面),这样就可以使其成为一个字段。
字段就像一个变量,但它对类中的所有方法都是可见的。
例如:
final ValueAnimator timern = ValueAnimator.ofInt(secondsToRun);
public void nezablokovat() {
int secondsToRun = 9999999;
timern.setDuration(secondsToRun * 1000).setInterpolator(new LinearInterpolator());
timern.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int elapsedSeconds = (int) animation.getAnimatedValue();
int minutes = elapsedSeconds / 60;
int seconds = elapsedSeconds % 60;
if (seconds%10 == 1) {
Pis("$$$");
}
}
});
timern.start();
}
void teplotahore() {
STup.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
timern.cancel();
}
}
});
}
关于java - 从方法中取消定时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35351716/