Android 台灣中文網

標題: 請各位幫幫忙(倒數計器修正) [打印本頁]

作者: kira800917    時間: 2013-5-14 16:44
標題: 請各位幫幫忙(倒數計器修正)

請問各位大大
倒數計時需要加入甚麼指令,才能讓他停止且停止後能恢復上次繼續倒數



public class MainActivity extends Activity implements OnClickListener
    {
        private static final String tag = "Main";
        private MalibuCountDownTimer countDownTimer;
        private long timeElapsed;
        private boolean timerHasStarted = false;
        private Button startB;
        private TextView text;
        private TextView timeElapsedView;
        //開始時間
        int startTime = 50000;
        //每秒
        int  interval = 1000;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                startB = (Button) this.findViewById(R.id.button);
                startB.setOnClickListener(this);

                text = (TextView) this.findViewById(R.id.timer);
                timeElapsedView = (TextView) this.findViewById(R.id.timeElapsed);
                countDownTimer = new MalibuCountDownTimer(startTime, interval);
                text.setText(text.getText() + String.valueOf(startTime));
            }

        @Override
        public void onClick(View v)
            {
                if (!timerHasStarted)
                    {
                        countDownTimer.start();
                        timerHasStarted = true;
                        startB.setText("Start");
                    }
                else
                    {

                        countDownTimer.cancel();
                        timerHasStarted = false;
                        startB.setText("RESET");
                    }
            }

        // CountDownTimer class
        public class MalibuCountDownTimer extends CountDownTimer
            {

                public MalibuCountDownTimer(int  startTime, int  interval)
                    {
                        super(startTime, interval);
                    }

                @Override
                public void onFinish()
                    {
                        text.setText("Time's up!");
                        timeElapsedView.setText("Time Elapsed: " +startTime);
                    }

                @Override
                public void onTick(long millisUntilFinished)
                    {
//                       
//                         timeElapsedView.setText("seconds remaining:" + millisUntilFinished
//                                                    / 1000);
                        text.setText("Time remain:" + millisUntilFinished);
                        timeElapsed = startTime - millisUntilFinished;
                        timeElapsedView.setText("Time Elapsed: " + timeElapsed);
                    }
            }
    }
作者: ikv.tw    時間: 2013-5-15 21:25
指令我忘記了,畢竟我好久沒寫了,你就設定一個迴圈DO LOOP之類的,時間到0時loop回程式起始點就可以了
幫得上忙的話。記得給我碎鑽




歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1