0

类似的查询:等到所有线程在java中完成它们的工作

您好,我正在开发包含某种倒数计时器的 android 应用程序。我的问题是,我有多个倒计时计时器,它们设置 TextView 的文本并且必须单独工作(第一个倒计时计时器必须等到第二个计时器完成等)

见代码:

MyCountDownTimer.java

public class MyCountdownTimer extends CountDownTimer {
TextView tv;

    // default constructor
    public MyCountdownTimer (long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    // constructor
    public MyCountdownTimer(int hours, int mins, int secs,
    long countDownInterval, TextView tv) {
        super(3600000 * hours + 60000 * mins + secs * 1000, countDownInterval);
        this.tv = tv;
            // all other settings
    }   

    // when finished, set text of textview to done
    @Override
    public void onFinish() {
        tv.setText("done!");
    }

    // when working periodically update text of text view to value of time left
    @Override
    public void onTick(long millisUntilFinished) {
tv.setText(/*function setting text of TextView based on time left*/);
    }
}

定时器.java

public class Timer extends Fragment {
Button bStart;
Button bStop;
TextView tvShowTime;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.timer, container, false);
}

public void onStart() {
    super.onStart();
    bStart = (Button) getView().findViewById(R.id.bTimerStart);
    bStop = (Button) getView().findViewById(R.id.bTimerStop);
    tvShowTime = (TextView) getView().findViewById(R.id.showTime);
    // setting on button start click
    bStart.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            timerStart();
        }
    });

    // setting on button stop click
    bStop.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            timerStop();
        }
    });
}
    private void timerStart() {
        bStart.setClickable(false);
        int repeat = 2;
        int hour, mins, secs;
        for (int i = 0; i < 2 * repeat; i++) {
            if (i % 2 == 0) {
                //  setting working count down timer values
                mins = 1;

            } else {
                // setting resting count down timer values  
                secs = 30;
                    }
            timerCount = new MyCountdownTimer(hours, mins, secs, REFRESH_RATE,
            tvShowTime);
            timerCount.start();
            //////////////////////////////////////////////////
            // HERE I WANT TO WAIT UNTIL COUNTDOWN IS DONE  //
            //   ATM SECOND, THIRD AND FOURTH COUNT DOWN    //
            //           TIMER IS STARTED                   //
            //////////////////////////////////////////////////
        }
}

- 编辑 -

最后我做了 2 个 CountDownTimers,第一个调用 onFinish() 第二个,第二个调用第一个,直到 repeat = 0; 最后,这对我来说是最好的解决方案。无论如何感谢您的帮助,@Triode 的回答对我帮助很大

4

2 回答 2

1

开始你SECOND, THIRD AND FOURTH COUNT DOWN TIMERonFinish()MyCountdownTimer

于 2013-04-22T10:00:42.447 回答
0

我认为您必须放下timerStop()方法实现并且不要试图隐藏它,您需要答案但您不希望其他人受益:s

于 2013-12-17T17:36:34.940 回答