在我的应用中,我的倒计时从五点到一点。此时,只要活动开始,计数器就会启动。我想要的是保持5秒钟并等待倒计时直到触摸屏幕。因此倒计时器应该启动触摸事件。
public class MainActivity extends Activity implements OnGestureListener {
private static final String FORMAT = "%02d:%02d";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
new CountDownTimer(5000, 10) {
public void onTick(long millisUntilFinished) {
text.setText("" + String.format("%02d:%03d",
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)),
TimeUnit.MILLISECONDS.toMillis(millisUntilFinished) - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished))
));
if (animationRunning) {
cancel();
}
}
public void onFinish() {
text.setText("done!");
}
}.start();
}
@Override
public boolean onTouchEvent(MotionEvent touchevent) {
}
}
答案 0 :(得分:1)
将倒数计时器放在onTouch内,如下所示
public class MainActivity extends Activity implements OnGestureListener {
private static final String FORMAT = "%02d:%02d";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
}
@Override
public boolean onTouchEvent(MotionEvent touchevent) {
new CountDownTimer(5000, 10) {
public void onTick(long millisUntilFinished) {
text.setText("" + String.format("%02d:%03d",
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)),
TimeUnit.MILLISECONDS.toMillis(millisUntilFinished) - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished))
));
if (animationRunning) {
cancel();
}
}
public void onFinish() {
text.setText("done!");
}
}.start();
}
}
答案 1 :(得分:0)
将此部分代码移至onTouchEvent,因为它在活动创建时就是为什么它在您的活动开始时启动
a
你应该没事