在Android中重复X次特定操作

时间:2016-04-21 15:43:49

标签: java android xml timer

所以我想要执行某些动作X次,取决于用户想要它多少次。用户选择将从使用以下代码设置的TextView字符串中选择:

    homeScreenPlus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dodajInterval();
            homeScreenMinus.setEnabled(counter > 0);
        }
    });

    homeScreenMinus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            oduzmiInterval();
            homeScreenMinus.setEnabled(counter > 0);
        }
    });


}

private void oduzmiInterval() {
    counter--;
    brojIntervala.setText(Integer.toString(counter));
}

private void dodajInterval() {
    counter++;
    brojIntervala.setText(Integer.toString(counter));
}

这些基本上是两个按钮,用于递增或递减TextView的值。

我想要执行X次的动作是:

 public void homeScreenStart(View view) {
        linearniLayoutSetup.setVisibility(View.GONE);

        new CountDownTimer(seekBarTimerDelay.getProgress() * 1000 + 100, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                updateTimer((int) millisUntilFinished / 1000);
            }

            @Override
            public void onFinish() {
                textViewTimerVrijeme.setText("00:00");
                countDownTimerTrci();

                karticaTimera.setBackgroundColor(getResources().getColor(R.color.kartica_trci));
                textViewTimerTrciHodajBlaBla.setText(getResources().getString(R.string.timer_trci));
            }
        }.start();
    }


    private void countDownTimerTrci() {
        toolbar.setBackgroundColor(getResources().getColor(R.color.kartica_trci));
        new CountDownTimer(seekBarIntervaliVisokogIntenziteta.getProgress() * 1000 + 100, 1000){

            @Override
            public void onTick(long millisUntilFinished) {
                updateTimer((int) millisUntilFinished / 1000);
            }

            @Override
            public void onFinish() {
                textViewTimerVrijeme.setText("00:00");
                karticaTimera.setBackgroundColor(getResources().getColor(R.color.kartica_hodaj));
                imageViewTimerSlika.setImageResource(R.drawable.ic_timer_niski_intenzitet);
                textViewTimerTrciHodajBlaBla.setText(getResources().getString(R.string.timer_hodaj));
                countDownTimerHodaj();
            }
        }.start();
    }

    private void countDownTimerHodaj(){
        toolbar.setBackgroundColor(getResources().getColor(R.color.kartica_hodaj));
        new CountDownTimer(seekBarIntervaliNiskogIntenziteta.getProgress() * 1000 + 100, 1000){

            @Override
            public void onTick(long millisUntilFinished) {
                updateTimer((int) millisUntilFinished / 1000);
            }

            @Override
            public void onFinish() {
                textViewTimerVrijeme.setText("00:00");
                karticaTimera.setBackgroundColor(getResources().getColor(R.color.colorAccent));
                textViewTimerTrciHodajBlaBla.setText("Done!");
            }
        }.start();
    }

所以它是3个搜索栏或计时器的组合,一个接一个地运行,然后它们应该运行用户选择的次数。

问题是,我不知道如何设置它,我需要在计时器中的第一个代码块只运行一次,并且它下面的那两个方法运行X次。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

除非我遗漏了某些东西,否则你只能将代码包含在引用当前计数器的循环中的操作中?您可能需要阻止输入,以便用户在执行开始后不能修改计数器(除非您希望它们),但应该是它。

public void homeScreenStart(View view) {
    linearniLayoutSetup.setVisibility(View.GONE);

    CountDownTimer firstCountDown = new CountDownTimer(seekBarTimerDelay.getProgress() * 1000 + 100, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            updateTimer((int) millisUntilFinished / 1000);
        }

        @Override
        public void onFinish() {
            textViewTimerVrijeme.setText("00:00");
            countDownTimerTrci();

            karticaTimera.setBackgroundColor(getResources().getColor(R.color.kartica_trci));
            textViewTimerTrciHodajBlaBla.setText(getResources().getString(R.string.timer_trci));
        }
    }

    CountDownTimer secondCountDown = new CountDownTimer(seekBarIntervaliVisokogIntenziteta.getProgress() * 1000 + 100, 1000){

        @Override
        public void onTick(long millisUntilFinished) {
            updateTimer((int) millisUntilFinished / 1000);
        }

        @Override
        public void onFinish() {
            textViewTimerVrijeme.setText("00:00");
            karticaTimera.setBackgroundColor(getResources().getColor(R.color.kartica_hodaj));
            imageViewTimerSlika.setImageResource(R.drawable.ic_timer_niski_intenzitet);
            textViewTimerTrciHodajBlaBla.setText(getResources().getString(R.string.timer_hodaj));
            countDownTimerHodaj();
        }
    }

    CountDownTimer thirdCountDown = new CountDownTimer(seekBarIntervaliVisokogIntenziteta.getProgress() * 1000 + 100, 1000){

        @Override
        public void onTick(long millisUntilFinished) {
            updateTimer((int) millisUntilFinished / 1000);
        }

        @Override
        public void onFinish() {
            textViewTimerVrijeme.setText("00:00");
            karticaTimera.setBackgroundColor(getResources().getColor(R.color.kartica_hodaj));
            imageViewTimerSlika.setImageResource(R.drawable.ic_timer_niski_intenzitet);
            textViewTimerTrciHodajBlaBla.setText(getResources().getString(R.string.timer_hodaj));
            countDownTimerHodaj();
        }
    }

    for (int i = 0; i < counter; i++) {
        firstCountDown.start();
        toolbar.setBackgroundColor(getResources().getColor(R.color.kartica_trci));
        secondCountDown.start();
        thirdCountDown.start();
    }
}

应该适合你