递增countDownTimer的Andr​​oid [英] Incrementing the countDownTimer Android

查看:109
本文介绍了递增countDownTimer的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个关于CountDownTimer问题。我必须做的一个应用程序,允许用户通过+1递增的时间的时间,对于按钮的每次点击。按钮停止被点击后,再等待三秒钟,然后开始倒计时。

have a question about CountDownTimer. I have to make an application that allows the user to increment the time time by +1, for every click of the button. Then after the button stops being clicked it waits for three seconds, then starts to countdown.

我贴我下面的code。

I pasted my code below.

我的问题是:我似乎无法得到正确的工作人员数量的递增,但看来以后我停止增加的数量(的onStop())直接进入(onFinish())。而不是要去OnTick()和1减少的数量每秒。我曾尝试多种方法来解决这个问题,但都被卡住了。

My Issue is: I can't seem to get the Incrementing of the number working correctly, however it seems that after I stop Incrementing the number (onStop()) it directly goes to (onFinish()). Instead of going to the OnTick() and decreasing the number by 1 every second. I have tried numerous ways to fix this, but have been stuck.

任何人都可以引导我做什么正确的方向?任何帮助将是AP preciated。谢谢你们!

Can anyone lead me in the right direction of what to do? Any help would be appreciated. Thank you guys!

public class MainActivity extends Activity {

    Button incrementTime, startTime;
    public TextView timedisplay;
    public myTimer wavetimer;
    private long millisInFuture;
    private long millisUntilFinished;
    private long countDownInterval;
    private long onclicktime;
    //private WaveInterface model;
    public int countdown;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     
    incrementTime = (Button) findViewById(R.id.button2);
    startTime = (Button) findViewById(R.id.button3);
    incrementTime.setText("Increment Time");
    startTime.setText("Start");
    timedisplay = (TextView) findViewById(R.id.mycounter);
    timedisplay.setText("Time Left: " + millisUntilFinished);
    wavetimer = new myTimer (millisInFuture, 1000);


    incrementTime.setOnClickListener(new OnClickListener(){
    int countdown = 01;
   /**
    * On click button Listner
 * @return 
    */
    public void onClick(View v) {
        millisInFuture++;
        return; 
    }

   });
    startTime.setOnClickListener(new OnClickListener(){
    public void onClick(View v) {
        wavetimer.start();
    }
});}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


public class myTimer extends CountDownTimer  {
public long millisInFuture;
private long countDownInterval;


public myTimer(long millisInFuture, long countDownInterval) {
    super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
    millisUntilFinished--;
    if (millisUntilFinished == 0){
        wavetimer.onFinish();
    }
    else {
    timedisplay.setText("Time Left: " + millisUntilFinished / 1000);
    }

}
@Override
public void onFinish() {
    timedisplay.setText("Countdown Finished");
    wavetimer.cancel();
}


public void onStop() {
    millisInFuture = millisInFuture + 1;
    timedisplay.setText("Time Left: " + millisInFuture);
}

我似乎无法弄清楚如何正确地递增的数量。我曾尝试多种方式,并不断搞砸了。不知道去哪里找。任何帮助将是AP preciated。谢谢你们!

I can't seem to figure out how to increment the number correctly. I have tried multiple ways and it keeps screwing up. Not sure where to look. Any help would be appreciated. Thank you guys!

推荐答案

您应该知道,您不必手动调用 onFinish 当时间到了,在倒计时一流的,无论你有你取消 onFinish 为一个导致另一个或手动递减任何东西。请尝试以下,看看它是否适合你脑子里想的任务。希望你可以注意到,我没有看到现实的原因,看是否有按钮没有被点击了。

You should know that you don't have to manually call onFinish when time is up in the countdown class, neither do you have you to cancel in onFinish as one leads to the other or manually decrement anything. Try the following and see if it fits the task that you had in mind. Hope you can note that i don't see pragmatic reason to see if a button "isn't being clicked anymore".

public class MainActivity extends Activity {

    Button incrementTime, startTime;
    public TextView timedisplay;
    long millisInFuture = 1000;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        incrementTime = (Button) findViewById(R.id.button2);
        startTime = (Button) findViewById(R.id.button3);
        timedisplay = (TextView) findViewById(R.id.mycounter);

        resetText();

        incrementTime.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                millisInFuture += 1000;
                resetText();
            }
        });

        startTime.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                CountDownTimer wavetimer = new myTimer(millisInFuture + 3000, 1000).start();
                // ^ add 3 seconds.
            }
        });}

    protected void resetText() {
        timedisplay.setText("Time Left: " + millisInFuture / 1000);
    }

    public class myTimer extends CountDownTimer  {

        private long millisActual;

        public myTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            millisActual = millisInFuture - 3000;
        }

        @Override
        public void onTick(long millisUntilFinished) {
            //v start showing the tick after 3 seconds.
            if (millisUntilFinished <= millisActual) {
                timedisplay.setText("Time Left: " + millisUntilFinished / 1000);                
            }
        }

        @Override
        public void onFinish() {
            timedisplay.setText("Countdown Finished");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

这篇关于递增countDownTimer的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆