跟踪倒数计时器每分钟 [英] Track each minute in countdown timer

查看:157
本文介绍了跟踪倒数计时器每分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想告诉每个minute.I完成现在用的倒数计时器后祝酒消息,并显示定时器在文本view.Please帮我把这个problem.Following理清我的code

  @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.track_me_mode);
    计数器=新mycount的(长度,10);
    counter.start();
    }
    });
         公共类mycount的扩展CountDownTimer {
        语境mContext;

        公共mycount的(长millisInFuture,长countDownInterval){
          超(mil​​lisInFuture,countDownInterval);
        }


        公共无效onTick(长millisUntilFinished){
            timerView.setText(formatTime(millisUntilFinished));


          }

        公共无效onFinish(){


              }

            }
公共字符串formatTime(long millis)来{
    输出=;
    长秒=米利斯/ 1000;
    长分=秒/ 60;
    长时间=分钟/ 60;

    秒=秒%60;
    分钟=分钟%60;
    小时=小时%60;

    字符串secondsD =将String.valueOf(秒);
    字符串minutesD =将String.valueOf(分钟);
    字符串hoursD =将String.valueOf(小时);

    的System.out.println(minutesD);

    如果(秒-1,10)
      secondsD =0+秒;
    如果(分钟小于10)
      minutesD =0+分钟;

    如果(小时小于10)
        hoursD =0+小时;

    输出= hoursD +:+ minutesD +:+ secondsD;
    返回输出;
  }
}
 

解决方案

好了,您需要更改code点点,

  1. 您需要更改秒变量一流水平
  2. 您需要更改区间如下,

    计数器=新mycount的(3600000,10); 计数器=新mycount的(3600000,1000); ,因为第二个参数是毫秒。我已经做了所有的变化,跌破code,

     公共类MainActivity扩展活动
    {
    TextView的timerView;
    按钮结束,SOS;
    字符串输出;
    mycount的计数器;
    INT长;
    
    //路西法更改
    长秒;
    
    @覆盖
    保护无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
    
        timerView =(TextView中)findViewById(R.id.tv_timer_track_me);
    
          //计数器=新mycount的(3600000,10);
            计数器=新mycount的(3600000,1000);
            counter.start();
    }
    
    公共字符串formatTime(long millis)来
    {
        输出=;
        秒=米利斯/ 1000;
        长分=秒/ 60;
        长时间=分钟/ 60;
    
        秒=秒%60;
        分钟=分钟%60;
        小时=小时%60;
    
        字符串secondsD =将String.valueOf(秒);
        字符串minutesD =将String.valueOf(分钟);
        字符串hoursD =将String.valueOf(小时);
    
        如果(秒-1,10)
            secondsD =0+秒;
        如果(分钟小于10)
            minutesD =0+分钟;
    
        如果(小时小于10)
            hoursD =0+小时;
    
        输出= hoursD +:+ minutesD +:+ secondsD;
    
        返回输出;
    }
    
    公共类mycount的扩展CountDownTimer
    {
        语境mContext;
    
        公共mycount的(长millisInFuture,长countDownInterval)
        {
            超(mil​​lisInFuture,countDownInterval);
        }
    
    
        公共无效onTick(长millisUntilFinished)
        {
            timerView.setText(formatTime(millisUntilFinished));
    
            如果(秒== 0)
            {
                Toast.makeText(getApplicationContext(),完成,Toast.LENGTH_LONG).show();
            }
        }
    
        公共无效onFinish(){}
    
    }
    
    @覆盖
    公共无效的onDestroy()
    {
        super.onDestroy();
        counter.cancel();
    }
    }
     

I want to show a toast message after completion of each minute.I am using the count down timer and show the timer in a text view.Please help me to sort out this problem.Following is my code.

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.track_me_mode);
    counter = new MyCount (length,10);
    counter.start();
    }
    });
         public class MyCount extends CountDownTimer {
        Context mContext;

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


        public void onTick (long millisUntilFinished) {
            timerView.setText ( formatTime(millisUntilFinished));


          }

        public void onFinish() {


              }

            }
public String formatTime(long millis) {
    output = "";
    long seconds = millis / 1000;
    long minutes = seconds / 60;
    long hours=minutes/ 60;

    seconds = seconds % 60;
    minutes = minutes % 60;
    hours=hours%60;

    String secondsD = String.valueOf(seconds);
    String minutesD = String.valueOf(minutes);
    String hoursD=String.valueOf(hours);

    System.out.println(minutesD);

    if (seconds < 10)
      secondsD = "0" + seconds;
    if (minutes < 10)
      minutesD = "0" + minutes;

    if (hours < 10)
        hoursD = "0" + hours;

    output = hoursD+" : "+minutesD + " : " + secondsD;
    return output;
  }
}

解决方案

Ok, You need to change your code little bit,

  1. You need to change your Seconds variable to class level
  2. You need to change your interval as follows,

    from counter = new MyCount (3600000,10); to counter = new MyCount (3600000,1000); because second argument is millisecond. I have done all the changes in below code,

    public class MainActivity extends Activity
    {
    TextView timerView;
    Button end,SOS;
    String output;
    MyCount counter;
    int length;
    
    // Change by Lucifer
    long seconds;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        timerView=(TextView)findViewById(R.id.tv_timer_track_me);
    
          //        counter = new MyCount (3600000,10);
            counter = new MyCount (3600000,1000);
            counter.start();
    }
    
    public String formatTime(long millis) 
    {
        output = "";
        seconds = millis / 1000;
        long minutes = seconds / 60;
        long hours=minutes/ 60;
    
        seconds = seconds % 60;
        minutes = minutes % 60;
        hours=hours%60;
    
        String secondsD = String.valueOf(seconds);
        String minutesD = String.valueOf(minutes);
        String hoursD=String.valueOf(hours);
    
        if (seconds < 10)
            secondsD = "0" + seconds;
        if (minutes < 10)
            minutesD = "0" + minutes;
    
        if (hours < 10)
            hoursD = "0" + hours;
    
        output = hoursD+" : "+minutesD + " : " + secondsD;
    
        return output;
    }
    
    public class MyCount extends CountDownTimer 
    {
        Context mContext;
    
        public MyCount(long millisInFuture, long countDownInterval) 
        {
            super(millisInFuture, countDownInterval);
        }
    
    
        public void onTick (long millisUntilFinished) 
        {
            timerView.setText ( formatTime(millisUntilFinished));
    
            if ( seconds == 0 )
            {
                Toast.makeText( getApplicationContext(), "Done", Toast.LENGTH_LONG ).show();
            }
        }
    
        public void onFinish() {}
    
    }
    
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        counter.cancel();
    }
    }
    

这篇关于跟踪倒数计时器每分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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