1

您好,我有一个计时器,我想在它结束后立即连续重新启动 4 次。

我想使用这样的 for 循环: for(int i = 0; i<=3; i++)

我的问题是,我不知道如何在我的代码中使用循环。我不知道在哪里添加 i 变量。

(我还添加了一个媒体播放器,它可以多次播放音频。您可能会注意到,我使用了 2 个 cntdowntimer。它是一个以 30 秒开始的间隔定时器,自动切换到开始 15 秒的第二个定时器,之后程序应该像我之前说的那样重新开始)

以下是我使用的方法:

public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnStart = (Button)findViewById(R.id.btnStart);
    btnStop = (Button)findViewById(R.id.btnStop);
    textViewTime = (TextView)findViewById(R.id.textViewTime);
    textViewTime.setText("00:00:00");

    mp = MediaPlayer.create(this, R.raw.vuvuneu);
    btnStart = (Button)this.findViewById(R.id.btnStart);



    counter=new CountDownTimer(15000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {

              ...
        }

        @Override
        public void onFinish() {

            textViewTime.setText("Done");
            mp.start();


        }
    };




    final CounterClass timer = new CounterClass(30000,1000);
    btnStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            timer.start();
            mp.start();

        }
    });
    btnStop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            timer.cancel();
        }
    });

}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
public class CounterClass extends CountDownTimer {
    public CounterClass(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }





        @Override
    public void onFinish() {
          counter.start();
            mp.start();



        }

提前致谢。:)

完整代码:

public class MainActivity extends ActionBarActivity {



    CountDownTimer counter;
    MediaPlayer mp;



    Button btnStart, btnStop;
    TextView textViewTime;
    @Override
    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnStart = (Button)findViewById(R.id.btnStart);
        btnStop = (Button)findViewById(R.id.btnStop);
        textViewTime = (TextView)findViewById(R.id.textViewTime);
        textViewTime.setText("00:00:00");

        mp = MediaPlayer.create(this, R.raw.vuvuneu);
        btnStart = (Button)this.findViewById(R.id.btnStart);



        counter=new CountDownTimer(15000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                long millis = millisUntilFinished;
                String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
                        TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                        TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
                System.out.println(hms);
                textViewTime.setText(hms);

            }

            @Override
            public void onFinish() {

                textViewTime.setText("Done");
                mp.start();


            }
        };




        final CounterClass timer = new CounterClass(30000,1000);
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.start();
                mp.start();

            }
        });
        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.cancel();
            }
        });

    }
    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @SuppressLint("NewApi")
    public class CounterClass extends CountDownTimer {
        public CounterClass(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }





            @Override
        public void onFinish() {
              counter.start();
                mp.start();



            }


        @SuppressLint("NewApi")
        @TargetApi(Build.VERSION_CODES.GINGERBREAD)
        @Override
        public void onTick(long millisUntilFinished) {

            textViewTime.setText("seconds remaining: " + millisUntilFinished / 1000);


            long millis = millisUntilFinished;
            String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
                    TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                    TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
            System.out.println(hms);
            textViewTime.setText(hms);
        }
    }
}
4

1 回答 1

1

试试这个不需要for循环的简单方法

public class MainActivity extends Activity {


    CountDownTimer workoutTimer;
    CountDownTimer restTimer;

    @Override
    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startWorkoutTimer(0);

            }
        });
        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (workoutTimer!=null ){
                    workoutTimer.cancel();
                }
                if (restTimer!=null ){
                    restTimer.cancel();
                }
            }
        });

    }


    public void startWorkoutTimer(final int count ){


        workoutTimer=new CountDownTimer(30000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {

                if(count!=3){
                    startRestTimer(count);
                }

            }
        };
        workoutTimer.start();

    }


    public void startRestTimer(final int count ){


        restTimer=new CountDownTimer(15000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {

                if(count!=3){
                    startWorkoutTimer(count+1);
                }

            }
        };
        restTimer.start();

    }




}
于 2014-08-09T12:19:55.487 回答