第一个问题:
我有 4 个倒计时(从不同的值开始),当我单击它们旁边的图像视图时应该开始。但问题是,当我点击第一张图片时,所有 4 个倒计时都开始了;当我单击第二张图片时,第二、第三和第四个倒计时开始,依此类推。
我想我在我的开关盒中做错了,但它没有显示任何错误,我找不到错误。
第二个问题:
当我单击“新游戏”按钮时,所有计时器都应该停止并变为 0,但是当我单击计时器时,它们会再次启动并显示其旧的剩余时间和新的剩余时间。
代码:
public class TimerActivity extends Activity implements
android.view.View.OnClickListener {
private CountDownTimer countDownTimer;
private Button button_new_fight;
public TextView textviewRed;
public TextView textviewBlue;
public TextView textviewDragon;
public TextView textviewBaron;
public TextView tv;
public ImageView imageviewRed;
public ImageView imageviewBlue;
public ImageView imageviewDragon;
public ImageView imageviewBaron;
private boolean timerRedStarted;
private boolean timerBlueStarted;
private boolean timerDragonStarted;
private boolean timerBaronStarted;
private final long startTimeRed = 300000;
private final long startTimeBlue = 300000;
private final long startTimeDragon = 360000;
private final long startTimeBaron = 420000;
private final long interval = 1000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timer_layout);
//binding content to these attributes to work with them
button_new_fight = (Button) this.findViewById(R.id.button1);
button_new_fight.setOnClickListener(this);
textviewRed = (TextView) this.findViewById(R.id.textViewRedTime);
textviewBlue = (TextView) this.findViewById(R.id.TextViewBlueTimeLeft);
textviewDragon = (TextView) this.findViewById(R.id.TextViewDragonTimeLeft);
textviewBaron = (TextView) this.findViewById(R.id.TextViewBaronTimeLeft);
//binding this.imageviews to those of timer_layout.xml
imageviewRed = (ImageView) this.findViewById(R.id.imageView1);
imageviewRed.setOnClickListener(this);
imageviewBlue = (ImageView) this.findViewById(R.id.imageView2);
imageviewBlue.setOnClickListener(this);
imageviewDragon = (ImageView) this.findViewById(R.id.imageView3);
imageviewDragon.setOnClickListener(this);
imageviewBaron = (ImageView) this.findViewById(R.id.imageView4);
imageviewBaron.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
// clear all timers
if (countDownTimer != null) {
countDownTimer.cancel();
countDownTimer = null;
}
// start timer red
case R.id.imageView1:
if (timerRedStarted == false) {
countDownTimer = new MyCount(startTimeRed, interval,
textviewRed);
textviewRed.setText(textviewRed.getText()
+ String.valueOf(startTimeRed));
countDownTimer.start();
}
// start timer blue
case R.id.imageView2:
if (timerBlueStarted == false) {
countDownTimer = new MyCount(startTimeBlue, interval,
textviewBlue);
countDownTimer.start();
}
// start timer dragon
case R.id.imageView3:
if (timerDragonStarted == false) {
countDownTimer = new MyCount(startTimeDragon, interval,
textviewDragon);
countDownTimer.start();
}
// start timer baron
case R.id.imageView4:
if (timerBaronStarted == false) {
countDownTimer = new MyCount(startTimeBaron, interval,
textviewBaron);
countDownTimer.start();
}
}
}
}
类 MyCount 扩展 CountDownTimer {
private TextView tv;
public MyCount(long millisInFuture, long countDownInterval, TextView tv) {
super(millisInFuture, countDownInterval);
this.tv = tv;
}
@Override
public void onTick(long millisUntilFinished) {
tv.setText("" + millisUntilFinished / 1000);
}