Android两个倒数计时器重叠;如何延迟第二个?

时间:2015-01-28 18:39:43

标签: android performance android-activity timer countdowntimer

我的代码由2个倒数计时器组成,一个完成后说GO!然后另一个开始,但第二个计时器立即启动,GO!消息立即消失,如何延迟GO!消息或第二个计时器的出现?这是代码。

public class WorkoutGymEasy1 extends Activity {

CountDownTimer cdt = null;
MediaPlayer mp = null;
TextView c;
Button b;
ImageView image;
TextView pushuptext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.workout_gym_easy1);
    final TextView c = (TextView) findViewById(R.id.timer_gym_easy1);
    c.setAlpha(0f);
    final Button b = (Button) findViewById(R.id.button1);
    b.setAlpha(1f);
    final ImageView image = (ImageView) findViewById(R.id.imagePushUp);
    b.setAlpha(1f);
    final TextView pushuptext = (TextView) findViewById(R.id.textPushUp);
    b.setAlpha(1f);

    RelativeLayout rl5 = (RelativeLayout) findViewById(R.id.rl5);
    rl5.setBackgroundColor(Color.BLACK);

     mp = MediaPlayer.create(WorkoutGymEasy1.this, R.raw.countdown);
        try {
            mp.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        b.setOnClickListener(new OnClickListener() {
            public void onClick (View v) {
                b.setAlpha(0f);
                c.setAlpha(1f);
                mp.start();
                new CountDownTimer(6000, 1000) { //20 seconds count down with 1s interval (1000 ms) //access TextView element on the screen to set the timer value
                    public void onTick(long millisUntilFinished) { // Code in this method is executed every 1000ms (1s)
                        c.setText("" + (millisUntilFinished / 1000) + ""); //update the timer
                        //if (millisUntilFinished == 0) {
                            //c.setText("GO!");
                        //}
                    }

                    @Override
                    public void onFinish() {
                        // TODO Auto-generated method stub
                        c.setText("GO!");
                        // Intent myIntent = new Intent(getApplicationContext(), WorkoutGymEasy2.class);
                        // startActivity(myIntent);
                        new CountDownTimer(31000, 1000) { //30 seconds count down with 1s interval (1000 ms) //access TextView element on the screen to set the timer value
                            public void onTick(long millisUntilFinished) { // Code in this method is executed every 1000ms (1s)
                                c.setText("" + (millisUntilFinished / 1000) + ""); //update the timer
                            }

                            @Override
                            public void onFinish() {
                                // TODO Auto-generated method stub
                                Intent myIntent = new Intent(getApplicationContext(), WorkoutGymEasy2.class);
                                startActivity(myIntent);

                                finish();               // call finish() method to destroy this activity and return to instructions screen
                            }
                        }.start();
                    }
                }.start();
            }
        });

}

protected void OnPause() {
    super.onPause();
    mp.release();
    finish();
}
}

1 个答案:

答案 0 :(得分:0)

您需要3个计时器才能模拟延迟。所以第一个计时器将开始和结束。开始" Go"计时器。然后当" Go"计时器结束,第二个正式计时器将关闭。