綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[其他] 請各位幫幫忙(倒數計器修正)

 關閉 [複製連結] 查看: 822|回覆: 1|好評: 0
跳轉到指定樓層
樓主
kira800917 | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2013-5-14 16:44
5碎鑽

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



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);
                    }
            }
    }

「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
ikv.tw | 收聽TA | 只看該作者
發表於 2013-5-15 21:25
指令我忘記了,畢竟我好久沒寫了,你就設定一個迴圈DO LOOP之類的,時間到0時loop回程式起始點就可以了
幫得上忙的話。記得給我碎鑽
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則