Android 如何等待应用程序直到倒计时结束

Android 如何等待应用程序直到倒计时结束,android,countdown,Android,Countdown,类似查询: 你好,, 我正在开发android应用程序,其中包含某种倒计时。我的问题是,我有多个倒计时,设置TextView的文本,并且必须单独工作(第一个倒计时必须等到第二个倒计时完成,等等) 见代码: MyCountDownTimer.java public class MyCountdownTimer extends CountDownTimer { TextView tv; // default constructor public MyCountdownTimer (

类似查询:

你好,, 我正在开发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*/);
    }
}
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                   //
            //////////////////////////////////////////////////
        }
}
Timer.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*/);
    }
}
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                   //
            //////////////////////////////////////////////////
        }
}
公共类计时器扩展片段{
按钮启动;
按钮bStop;
文本视图tvShowTime;
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
返回充气机。充气(右布局计时器,容器,假);
}
public void onStart(){
super.onStart();
bStart=(按钮)getView().findviewbyd(R.id.bTimerStart);
bStop=(按钮)getView().findViewById(R.id.bTimerStop);
tvShowTime=(TextView)getView().findViewById(R.id.showTime);
//设置按钮开始点击
bStart.setOnClickListener(新建按钮.OnClickListener(){
公共void onClick(视图v){
timerStart();
}
});
//设置按钮停止点击
bStop.setOnClickListener(新建按钮.OnClickListener(){
公共void onClick(视图v){
timerStop();
}
});
}
私有void timerStart(){
b开始设置可点击(错误);
int repeat=2;
整小时,分,秒;
对于(int i=0;i<2*重复;i++){
如果(i%2==0){
//设置工作倒计时计时器值
分钟=1;
}否则{
//设置静止倒计时计时器值
秒=30;
}
timerCount=新的MyCountdownTimer(小时、分钟、秒、刷新率、,
电视节目时间);
timerCount.start();
//////////////////////////////////////////////////
//我想在这里等到倒计时结束//
//ATM第二、第三和第四次倒计时//
//计时器启动//
//////////////////////////////////////////////////
}
}
--编辑--

最后我做了两个倒计时,第一个调用onFinish()中的第二个,第二个调用第一个,直到repeat=0;
最后,这对我来说是最好的解决办法。无论如何,谢谢你的帮助,@Triode的回答帮了我很大的忙

在你的
MyCountdownTimer的
onFinish()
中启动你的
第二、第三和第四个倒计时方法实现,不要试图隐藏它,您需要答案,但不希望其他人从中受益:s