简体   繁体   English

为什么单击按钮时我的计时器没有停止?

[英]Why aren't my timers stopping when button is clicked?

It was working fine when it was just one timer but when I added inserted a switch and added two more timers, the stop timer button seized working. 当它只是一个计时器时,它运行良好,但是当我添加了一个开关并添加了两个计时器时,停止计时器按钮开始起作用。 All the code is pasted below: 所有代码都粘贴在下面:

public class timer extends Activity implements OnClickListener {

  private Button buttonStartTime, buttonStopTime, buttonStartTime2, buttonStopTime2, buttonStartTime3, buttonStopTime3;
  private EditText edtTimerValue, edtTimerValue2, edtTimerValue3;
  private TextView textViewShowTime, textViewShowTime2, textViewShowTime3; // will show the time
  private CountDownTimer countDownTimer, countDownTimer2, countDownTimer3; // built in android class
  // CountDownTimer
  private long totalTimeCountInMilliseconds, totalTimeCountInMilliseconds2, totalTimeCountInMilliseconds3; // total count down time in
  // milliseconds
  private long timeBlinkInMilliseconds, timeBlinkInMilliseconds2, timeBlinkInMilliseconds3; // start time of start blinking
  private boolean blink, blink2, blink3; // controls the blinking .. on and off

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timer);

    buttonStartTime = (Button) findViewById(R.id.btnStartTime);
    buttonStopTime = (Button) findViewById(R.id.btnStopTime);
    textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
    edtTimerValue = (EditText) findViewById(R.id.edtTimerValue);

    buttonStartTime2 = (Button) findViewById(R.id.btnStartTime2);
    buttonStopTime2 = (Button) findViewById(R.id.btnStopTime2);
    textViewShowTime2 = (TextView) findViewById(R.id.tvTimeCount2);
    edtTimerValue2 = (EditText) findViewById(R.id.edtTimerValue2);

    buttonStartTime3 = (Button) findViewById(R.id.btnStartTime3);
    buttonStopTime3 = (Button) findViewById(R.id.btnStopTime3);
    textViewShowTime3 = (TextView) findViewById(R.id.tvTimeCount3);
    edtTimerValue3 = (EditText) findViewById(R.id.edtTimerValue3);

    buttonStartTime.setOnClickListener(this);
    buttonStopTime.setOnClickListener(this);

    buttonStartTime2.setOnClickListener(this);
    buttonStopTime2.setOnClickListener(this);

    buttonStartTime3.setOnClickListener(this);
    buttonStopTime3.setOnClickListener(this);

  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btnStartTime:
            if (v.getId() == R.id.btnStartTime) {
                textViewShowTime.setTextAppearance(getApplicationContext(),
                        R.style.normalText);
                setTimer();
                buttonStopTime.setVisibility(View.VISIBLE);
                buttonStartTime.setVisibility(View.GONE);
                edtTimerValue.setVisibility(View.GONE);
                edtTimerValue.setText("");
                startTimer();

            } else if (v.getId() == R.id.btnStopTime) {
                countDownTimer.cancel();
                buttonStartTime.setVisibility(View.VISIBLE);
                buttonStopTime.setVisibility(View.GONE);
                edtTimerValue.setVisibility(View.VISIBLE);
            }
            break;
        case R.id.btnStartTime2:
            if (v.getId() == R.id.btnStartTime2) {
                textViewShowTime2.setTextAppearance(getApplicationContext(),
                        R.style.normalText);
                setTimer2();
                buttonStopTime2.setVisibility(View.VISIBLE);
                buttonStartTime2.setVisibility(View.GONE);
                edtTimerValue2.setVisibility(View.GONE);
                edtTimerValue2.setText("");
                startTimer2();

            } else if (v.getId() == R.id.btnStopTime2) {
                countDownTimer2.cancel();
                buttonStartTime2.setVisibility(View.VISIBLE);
                buttonStopTime2.setVisibility(View.GONE);
                edtTimerValue2.setVisibility(View.VISIBLE);
            }
            break;
        case R.id.btnStartTime3:
            if (v.getId() == R.id.btnStartTime3) {
                textViewShowTime3.setTextAppearance(getApplicationContext(),
                        R.style.normalText);
                setTimer3();
                buttonStopTime3.setVisibility(View.VISIBLE);
                buttonStartTime3.setVisibility(View.GONE);
                edtTimerValue3.setVisibility(View.GONE);
                edtTimerValue3.setText("");
                startTimer3();

            } else if (v.getId() == R.id.btnStopTime) {
                countDownTimer3.cancel();
                buttonStartTime3.setVisibility(View.VISIBLE);
                buttonStopTime3.setVisibility(View.GONE);
                edtTimerValue3.setVisibility(View.VISIBLE);
            }
            break;

    }
  }

  private void setTimer() {
    int time = 0;
    if (!edtTimerValue.getText().toString().equals("")) {
        time = Integer.parseInt(edtTimerValue.getText().toString());
    } else
        Toast.makeText(timer.this, "Please Enter Minutes...",
                Toast.LENGTH_LONG).show();

    totalTimeCountInMilliseconds = 60 * time * 1000;

    timeBlinkInMilliseconds = 30 * 1000;
  }

  private void startTimer() {
    countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
        // 500 means, onTick function will be called at every 500
        // milliseconds

        @Override
        public void onTick(long leftTimeInMilliseconds) {
            long seconds = leftTimeInMilliseconds / 1000;

            if (leftTimeInMilliseconds < timeBlinkInMilliseconds) {
                textViewShowTime.setTextAppearance(getApplicationContext(),
                        R.style.blinkText);
                // change the style of the textview .. giving a red
                // alert style

                if (blink) {
                    textViewShowTime.setVisibility(View.VISIBLE);
                    // if blink is true, textview will be visible
                } else {
                    textViewShowTime.setVisibility(View.INVISIBLE);
                }

                blink = !blink; // toggle the value of blink
            }

            textViewShowTime.setText(String.format("%02d", seconds / 60)
                    + ":" + String.format("%02d", seconds % 60));
            // format the textview to show the easily readable format

        }

        @Override
        public void onFinish() {
            // this function will be called when the timecount is finished
            textViewShowTime.setText("Time up!");
            textViewShowTime.setVisibility(View.VISIBLE);
            buttonStartTime.setVisibility(View.VISIBLE);
            buttonStopTime.setVisibility(View.GONE);
            edtTimerValue.setVisibility(View.VISIBLE);
        }

    }.start();

  }
  private void setTimer2() {
    int time = 0;
    if (!edtTimerValue2.getText().toString().equals("")) {
        time = Integer.parseInt(edtTimerValue2.getText().toString());
    } else
        Toast.makeText(timer.this, "Please Enter Minutes...",
                Toast.LENGTH_LONG).show();

    totalTimeCountInMilliseconds2 = 60 * time * 1000;

    timeBlinkInMilliseconds2 = 30 * 1000;
  }

  private void startTimer2() {
    countDownTimer2 = new CountDownTimer(totalTimeCountInMilliseconds2, 500) {
        // 500 means, onTick function will be called at every 500
        // milliseconds

        @Override
        public void onTick(long leftTimeInMilliseconds) {
            long seconds = leftTimeInMilliseconds / 1000;

            if (leftTimeInMilliseconds < timeBlinkInMilliseconds2) {
                textViewShowTime2.setTextAppearance(getApplicationContext(),
                        R.style.blinkText);
                // change the style of the textview .. giving a red
                // alert style

                if (blink2) {
                    textViewShowTime2.setVisibility(View.VISIBLE);
                    // if blink is true, textview will be visible
                } else {
                    textViewShowTime2.setVisibility(View.INVISIBLE);
                }

                blink2 = !blink; // toggle the value of blink
            }

            textViewShowTime2.setText(String.format("%02d", seconds / 60)
                    + ":" + String.format("%02d", seconds % 60));
            // format the textview to show the easily readable format

        }

        @Override
        public void onFinish() {
            // this function will be called when the timecount is finished
            textViewShowTime2.setText("Time up!");
            textViewShowTime2.setVisibility(View.VISIBLE);
            buttonStartTime2.setVisibility(View.VISIBLE);
            buttonStopTime2.setVisibility(View.GONE);
            edtTimerValue2.setVisibility(View.VISIBLE);
        }

    }.start();
  }

  private void setTimer3() {
    int time = 0;
    if (!edtTimerValue3.getText().toString().equals("")) {
        time = Integer.parseInt(edtTimerValue3.getText().toString());
    } else
        Toast.makeText(timer.this, "Please Enter Minutes...",
                Toast.LENGTH_LONG).show();

    totalTimeCountInMilliseconds3 = 60 * time * 1000;

    timeBlinkInMilliseconds3 = 30 * 1000;
  }

  private void startTimer3() {
    countDownTimer3 = new CountDownTimer(totalTimeCountInMilliseconds3, 500) {
        // 500 means, onTick function will be called at every 500
        // milliseconds

        @Override
        public void onTick(long leftTimeInMilliseconds3) {
            long seconds = leftTimeInMilliseconds3 / 1000;

            if (leftTimeInMilliseconds3 < timeBlinkInMilliseconds3) {
                textViewShowTime3.setTextAppearance(getApplicationContext(),
                        R.style.blinkText);
                // change the style of the textview .. giving a red
                // alert style

                if (blink3) {
                    textViewShowTime3.setVisibility(View.VISIBLE);
                    // if blink is true, textview will be visible
                } else {
                    textViewShowTime3.setVisibility(View.INVISIBLE);
                }

                blink3 = !blink; // toggle the value of blink
            }

            textViewShowTime3.setText(String.format("%02d", seconds / 60)
                    + ":" + String.format("%02d", seconds % 60));
            // format the textview to show the easily readable format

        }

        @Override
        public void onFinish() {
            // this function will be called when the timecount is finished
            textViewShowTime3.setText("Time up!");
            textViewShowTime3.setVisibility(View.VISIBLE);
            buttonStartTime3.setVisibility(View.VISIBLE);
            buttonStopTime3.setVisibility(View.GONE);
            edtTimerValue3.setVisibility(View.VISIBLE);
        }

    }.start();
  }
}

You need to switch on stopping buttons as well. 您还需要打开停止按钮。

switch (v.getId()) {
    case R.id.btnStartTime:

means v.getId() == R.id.btnStartTime inside this case so checking the value of v.getId() there is useless. 意味着在这种情况下v.getId() == R.id.btnStartTime因此检查v.getId()的值是没有用的。 The first if ( v.getId() == R.id.btnStartTime ) will always be true, the second one ( v.getId() == R.id.btnStopTime ) always false. 第一个if( v.getId() == R.id.btnStartTime )始终为true,第二个v.getId() == R.id.btnStopTime )始终为false。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM