CountDownTimer-用户增量。问题 [英] CountDownTimer- that the user increments. Issues

查看:117
本文介绍了CountDownTimer-用户增量。问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我贴我下面的code。

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

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

  @燮pressWarnings(未使用)
    公共类MainActivity延伸活动{
        公众诠释倒计时;
        按钮停止时间;
        公众的TextView timedisplay;
        公共定时器wavetimer;
        专用长millisInFuture;
        专用长countDownInterval;
        专用长onclicktime;
        私人长时间完成;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        倒计时= 01;
        停止时间=(按钮)findViewById(R.id.button2);
        stoptime.setText(停止计时器);

        timedisplay =(TextView中)findViewById(R.id.mycounter);
        timedisplay.setText(0);
        wavetimer =新的定时器(millisInFuture,1000);
        完成= 0;
        stoptime.setOnClickListener(新OnClickListener(){

        公共无效的onClick(查看为arg0){
            wavetimer.onStop();
            //尝试{
            //视频下载(3000);
            // wavetimer.start();

            //}赶上(InterruptedException异常E){

            // e.printStackTrace();
        //}


            //wavetimer.onTick(millisInFuture);
        }

       });


    }



    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }



    公共类定时器扩展CountDownTimer {
    众长millisInFuture;
    专用长countDownInterval = 1000;
    专用长currentelapsed;
    专用长methodlimit;
    专用长lapsedperiod;

    公共定时器(长millisInFuture,长countDownInterval){
        超(mil​​lisInFuture,countDownInterval);
    }


    公共无效onFinish(){
        如果(millisInFuture == 0){
        timedisplay.setText(倒计时完成);
        } 其他 {
            timedisplay.setText(错误);

        }
    }

    公共无效的onStop(){
        wavetimer.cancel();
        millisInFuture = millisInFuture + 1;
        timedisplay.setText(剩余时间+ millisInFuture);

    }



    公共无效onTick(长millisUntilFinished){
            millisInFuture =(millisInFuture  -  1);
            timedisplay.setText(剩余时间+ millisInFuture / 1000);

        }

    }

}
 

解决方案

添加此到code;

  wavetimer.start();
 

:)

I 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.

I pasted my code below.

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.

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

 @SuppressWarnings("unused")
    public class MainActivity extends Activity {
        public int countdown;
        Button stoptime;
        public TextView timedisplay;
        public Timer wavetimer;
        private long millisInFuture;
        private long countDownInterval;
        private long onclicktime;
        private long finished;

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

        countdown = 01;
        stoptime = (Button) findViewById(R.id.button2);
        stoptime.setText("Stop Timer");

        timedisplay = (TextView) findViewById(R.id.mycounter);
        timedisplay.setText("0");
        wavetimer = new Timer (millisInFuture, 1000);
        finished = 0;
        stoptime.setOnClickListener(new OnClickListener(){

        public void onClick(View arg0) {
            wavetimer.onStop();
            //try{
            //  Thread.sleep(3000);
            //  wavetimer.start();

            //} catch (InterruptedException e) {

            //  e.printStackTrace();
        //  }


            //wavetimer.onTick(millisInFuture);
        }   

       });


    }



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



    public class Timer extends CountDownTimer  {
    public long millisInFuture;
    private long countDownInterval = 1000;
    private long currentelapsed;
    private long methodlimit;
    private long lapsedperiod;

    public Timer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }


    public void onFinish() {
        if (millisInFuture == 0){
        timedisplay.setText("Countdown Finished");
        } else {
            timedisplay.setText("error");

        }
    }

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

    }



    public void onTick(long millisUntilFinished) {
            millisInFuture = (millisInFuture - 1);
            timedisplay.setText("Time Left: " + millisInFuture / 1000);

        }

    } 

}

解决方案

add this into your code;

wavetimer.start();

:)

这篇关于CountDownTimer-用户增量。问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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