如何在Android中实现两个倒计时器

时间:2016-11-17 09:04:41

标签: android countdowntimer

我已经实现了第一个计数器的一个计数器和onFinish(),我启动了第二个计数器,但第一个计数器无法完成。文本“Bye Guyz”保留了一段时间,所以如何完成文本。

请帮帮我。

提前致谢。!!!

代码: -

counter= new CountDownTimer(10000, 1000) {

                public void onTick(long millisUntilFinished) {
                    if (count == 0) {
                        tv.setText("First counter");
                        tv2.setVisibility(View.VISIBLE);
                        tv2.setText("Hello Guyz");
                    }
                }

                public void onFinish() {
                    if(!flag) {
                        tv2.setText("Bye Guyz");
                        count = 0;
                        try {
                            counter.cancel();
                        }catch (Exception e){}
                    }
                    else if(flag) {
                       counter1 = new CountDownTimer(9000, 1000) {
                            public void onTick(long millisUntilFinished) {
                                flag = false;
                                tv.setText("Second counter");
                        tv2.setVisibility(View.VISIBLE);
                        tv2.setText("Hello Girls");
                                count = 0;
                            }

                            public void onFinish() {
                                tv2.setVisibility(View.VISIBLE);
                                tv2.setText("Bye Girls");
                                count = 0;
                            }
                        }.start();

3 个答案:

答案 0 :(得分:1)

  • 你有"调试"代码以确保代码到达counter1 = new CountDownTimer(9000, 1000)

  • 您确定第一个counter何时到达onFinish() flag变量为真?

  • 为什么当显然计数器已经结束时,您会在counter.cancel()中致电onFinish()

public void onFinish() { if(!flag) { tv2.setText("Bye Guyz"); count = 0; try { counter.cancel(); }catch (Exception e){} }

答案 1 :(得分:0)

如果你说你的tv2显示" Bye Guyz"这意味着你的旗帜设置为false,所以"否则如果"部分未被执行。 onFinish()只执行一次,因此您需要确保将该标志设置为true以启动第二个计数器。

此外,你不应该在onFinish()中取消你的计数器,因为它已经完成了。

答案 2 :(得分:0)

这是我的替代方案如下

创建自定义Counter扩展Thread

class Counter extends Thread {
    private long timeOne, timeTwo;
    private OnCounterFinishedListener mCounterFinishedListener;
    private Thread t;
    Activity activity = null;


    Counter(Context context){
        t = new Thread(this);
        activity = (Activity)context;
    }

    @Override
    public void run() {
        try {
            sleep(timeOne);
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mCounterFinishedListener.firstCounterFinished();
                }
            });

            sleep(timeTwo);
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mCounterFinishedListener.secondCounterFinished();
                }
            });

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

     void setTimes(long timeOne, long timeTwo){
        this.timeOne = timeOne;
        this.timeTwo = timeTwo;
    }

    public void start(OnCounterFinishedListener listener){
        mCounterFinishedListener = listener;
        t.start();
    }




    interface OnCounterFinishedListener{
        void firstCounterFinished();
        void secondCounterFinished();
    }
}

然后在你的主线程中你可以启动这个计数器

final Counter counter = new Counter(this);
counter.setTimes(5000, 5000);
counter.start(new Counter.OnCounterFinishedListener() {
    @Override
    public void firstCounterFinished() {
        // Update your first TextView
    }

    @Override
    public void secondCounterFinished() {
        // Update your second TextView
    }
});