【问题标题】:Android two countdown timer overlaps; How to delay the second one?Android 两个倒数计时器重叠;如何延迟第二个?
【发布时间】:2015-01-28 18:39:43
【问题描述】:

我的代码包含 2 个倒数计时器,一个在完成后说 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();
}
}

【问题讨论】:

    标签: android performance android-activity timer countdowntimer


    【解决方案1】:

    您需要 3 个计时器来模拟延迟。所以第一个计时器将开始和结束。启动“Go”计时器。然后当“Go”计时器结束时,第二个官方计时器将关闭。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-17
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 2020-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多