其他分享
首页 > 其他分享> > android – 在倒数计时器中跟踪每一分钟

android – 在倒数计时器中跟踪每一分钟

作者:互联网

我希望在每分钟完成后显示一个Toast消息.我正在使用倒计时器并在文本视图中显示计时器.请帮我解决这个问题.以下是我的代码.

    @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;
  }
}

解决方法:

好的,你需要稍微改变你的代码,

>您需要将Seconds变量更改为类级别
>您需要更改您的间隔,如下所示,

来自counter = new MyCount(3600000,10); to counter = new MyCount(3600000,1000);因为第二个参数是毫秒.我在下面的代码中完成了所有更改,

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();
}
}

标签:android,countdowntimer
来源: https://codeday.me/bug/20191006/1859629.html