在android中创建一个简单的数字计时器

发布于 2024-10-10 02:25:32 字数 68 浏览 0 评论 0原文

我基本上只是想跟踪从应用程序启动时的 0:00 开始的秒数和分钟数,并将其显示在 TextView 中,最好的方法是什么?

I'm basically looking to just keep track of seconds and minutes starting at 0:00 from when the app starts, and display it in a TextView what would be the best method of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

携君以终年 2024-10-17 02:25:32

当计时器启动一个非 UI 线程时,我宁愿不使用它来更新 TextView。在您的情况下,Timer 可以替换为 Handler 类。而且您还可以避免这个新的非 UI 线程的开销。 这里是一个示例:

private Handler mHandler = new Handler();

OnClickListener mStartListener = new OnClickListener() 
{
   public void onClick(View v)
   {
      if (mStartTime == 0L) 
      {
        mStartTime = System.currentTimeMillis();
        mHandler.removeCallbacks(mUpdateTimeTask);
        mHandler.postDelayed(mUpdateTimeTask, 100);
      }
   }
};

.........

private Runnable mUpdateTimeTask = new Runnable() 
{
   public void run() 
   {
     final long start = mStartTime;
     long millis = SystemClock.uptimeMillis() - start;
     int seconds = (int) (millis / 1000);
     int minutes = seconds / 60;
     seconds     = seconds % 60;

     if (seconds < 10) 
     {
        mTimeLabel.setText("" + minutes + ":0" + seconds);
     } 
     else 
     {
        mTimeLabel.setText("" + minutes + ":" + seconds);            
     }

     mHandler.postAtTime(this,start + (((minutes * 60) + seconds + 1) * 1000));
   }
}; 

As timer starts a non-UI Thread, i'll prefer not to use it for updating TextView. In your case Timer can be replaced by the Handler class. And you can also avoid the overhead of this new non-UI thread. Here is an example:

private Handler mHandler = new Handler();

OnClickListener mStartListener = new OnClickListener() 
{
   public void onClick(View v)
   {
      if (mStartTime == 0L) 
      {
        mStartTime = System.currentTimeMillis();
        mHandler.removeCallbacks(mUpdateTimeTask);
        mHandler.postDelayed(mUpdateTimeTask, 100);
      }
   }
};

.........

private Runnable mUpdateTimeTask = new Runnable() 
{
   public void run() 
   {
     final long start = mStartTime;
     long millis = SystemClock.uptimeMillis() - start;
     int seconds = (int) (millis / 1000);
     int minutes = seconds / 60;
     seconds     = seconds % 60;

     if (seconds < 10) 
     {
        mTimeLabel.setText("" + minutes + ":0" + seconds);
     } 
     else 
     {
        mTimeLabel.setText("" + minutes + ":" + seconds);            
     }

     mHandler.postAtTime(this,start + (((minutes * 60) + seconds + 1) * 1000));
   }
}; 
凉宸 2024-10-17 02:25:32

您可以使用 Timer 来安排 TimerTask 每秒。

  • 首先获取开始时间,
  • 然后在 TimerTask 作业中获取所花费的时间(开始时间 - 当前时间),
  • 将此差异格式化为 xx:xx 形式的字符串,其中 DateFormat
  • 您使用此字符串更新 TextView

我不确定您在更新时是否会遇到问题TimerTask 作业中的 TextView,因为它不在 UI 线程中运行,在这种情况下,您可以使用 Activity.runOnUiThread 来执行此操作

You could use a Timer to schedule a TimerTask each second.

  • first you take the beginning time
  • then in the TimerTask job you get the time spent (beginning time - current time)
  • you format this difference as a String of the form xx:xx with DateFormat
  • you update the TextView with this String

I'm not sure if you could have problems when updating the TextView from the TimerTask job as it doesn't run in UI thread, in that case you can use Activity.runOnUiThread to do this

孤寂小茶 2024-10-17 02:25:32
    Runnable run = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            long millis = System.currentTimeMillis() - starttime;
               int seconds = (int) (millis / 1000);
               int minutes = (seconds%3600)/60;
               int hours = seconds / 3600;
               seconds     = seconds % 60;

               et1.setText(String.format("%02d:%02d:%02d",hours, minutes, seconds));
     //          et2.setText(String.format("%02d:%02d:%02d",hours, minutes, seconds));
      //         et3.setText(String.format("%02d:%02d:%02d",hours, minutes, seconds));

               h2.postDelayed(this, 500);
        }
    };

    class firstTask extends TimerTask {

        public void run() {
            handler.sendEmptyMessage(0);
        }
};  

在你的程序中尝试一下...!

    Runnable run = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            long millis = System.currentTimeMillis() - starttime;
               int seconds = (int) (millis / 1000);
               int minutes = (seconds%3600)/60;
               int hours = seconds / 3600;
               seconds     = seconds % 60;

               et1.setText(String.format("%02d:%02d:%02d",hours, minutes, seconds));
     //          et2.setText(String.format("%02d:%02d:%02d",hours, minutes, seconds));
      //         et3.setText(String.format("%02d:%02d:%02d",hours, minutes, seconds));

               h2.postDelayed(this, 500);
        }
    };

    class firstTask extends TimerTask {

        public void run() {
            handler.sendEmptyMessage(0);
        }
};  

try it in ur program...!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文