android 中的秒表问题

发布于 2024-11-06 02:54:49 字数 2512 浏览 0 评论 0原文

在我的应用程序中,我放置了一个带有两个按钮的秒表。秒表以正确的方式工作。但问题是,计时器在启动时看起来为 0:0:0,当它开始计数时,个位数会变为两位数,如 0:12:53。

这也会影响和干扰其他层。在开始时,我希望它默认显示为 00:00:00,这样我就无法更改布局。

但我不知道在哪里给出这个值。以下是我的代码

  b1.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View view) 
            {
                if(b1.getText().toString().equals("Start"))
                {
                        if(currentThread.getState()==Thread.State.NEW)
                        {
                            currentThread.start();
                            shouldRun = true;
                            b1.setText("Stop");
                        }
                        else
                        {
                          shouldRun = false;
                          b1.setText("Stop");
                        }
                }
                else if(b1.getText().toString().equals("Stop"))
                {
                    time=0;
                    time1=0;
                    time2=0;
                }
            }
        });     
        b2.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View view) 
            {
                if(b2.getText().toString().equals("Pause"))
                {
                    shouldRun = false;
                    b2.setText("Resume");
                }
                else if(b2.getText().toString().equals("Resume"))
                {
                    shouldRun = true;
                    b2.setText("Pause");
                }
            }
        });               
    }
    @Override
    public void run()
    {
        try 
        {
         while(true)
         {  
            while(shouldRun)
            {
                Thread.sleep(1000);
                Log.e("run", "run");
                threadHandler.sendEmptyMessage(0);
            }
         }   
        } 
        catch (InterruptedException e) {}
    }


    private Handler threadHandler = new Handler() 
    {
        public void handleMessage(android.os.Message msg) 
        {
            sec=time2++;
            if(sec == 59)
            {
                time2=0;
                sec = time2++;
                min=time1++;
            }
            if(min == 59)
            {
                time1=0;
                min = time1++;
                hr=time3++;
            }             
            stopWatch.setText(""+hr+":"+min+":"+sec);
        }
    };

}

如何做到这一点请帮助我............

in my app i have placed a stop watch with two buttons. The stop watch works in a proper way. But the problem is at start the timer looks as 0:0:0, when it starts counting the single digits are been changed over to double digits as 0:12:53.

this affects and disturbs the other layers too. At the start itself i want it to be displayed as 00:00:00 by default, so that i cant make changes in the layout.

but i don't know where to give the value for this. Following is my code

  b1.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View view) 
            {
                if(b1.getText().toString().equals("Start"))
                {
                        if(currentThread.getState()==Thread.State.NEW)
                        {
                            currentThread.start();
                            shouldRun = true;
                            b1.setText("Stop");
                        }
                        else
                        {
                          shouldRun = false;
                          b1.setText("Stop");
                        }
                }
                else if(b1.getText().toString().equals("Stop"))
                {
                    time=0;
                    time1=0;
                    time2=0;
                }
            }
        });     
        b2.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View view) 
            {
                if(b2.getText().toString().equals("Pause"))
                {
                    shouldRun = false;
                    b2.setText("Resume");
                }
                else if(b2.getText().toString().equals("Resume"))
                {
                    shouldRun = true;
                    b2.setText("Pause");
                }
            }
        });               
    }
    @Override
    public void run()
    {
        try 
        {
         while(true)
         {  
            while(shouldRun)
            {
                Thread.sleep(1000);
                Log.e("run", "run");
                threadHandler.sendEmptyMessage(0);
            }
         }   
        } 
        catch (InterruptedException e) {}
    }


    private Handler threadHandler = new Handler() 
    {
        public void handleMessage(android.os.Message msg) 
        {
            sec=time2++;
            if(sec == 59)
            {
                time2=0;
                sec = time2++;
                min=time1++;
            }
            if(min == 59)
            {
                time1=0;
                min = time1++;
                hr=time3++;
            }             
            stopWatch.setText(""+hr+":"+min+":"+sec);
        }
    };

}

how to do this please help me............

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

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

发布评论

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

评论(4

那支青花 2024-11-13 02:54:49

这应该可以完成工作:

String time = String.format("%02d:%02d:%02d", hr, min, sec);
stopWatch.setText(time);

或者单行版本:

stopWatch.setText(String.format("%02d:%02d:%02d", hr, min, sec));

This should do the work:

String time = String.format("%02d:%02d:%02d", hr, min, sec);
stopWatch.setText(time);

Or the one-line-version:

stopWatch.setText(String.format("%02d:%02d:%02d", hr, min, sec));
゛时过境迁 2024-11-13 02:54:49

像这样设置文本

stopWatch.setText(""+(hr<10?"0"+hr:hr)+":"+(min<10?"0"+min:min)+":"+(sec<10?"0"+sec:sec));

set the text like so

stopWatch.setText(""+(hr<10?"0"+hr:hr)+":"+(min<10?"0"+min:min)+":"+(sec<10?"0"+sec:sec));
好倦 2024-11-13 02:54:49

will:

stopWatch.setText("00:00:00");

初始化时不这样做吗?

will:

stopWatch.setText("00:00:00");

not do the trick when you initialise it?

漫漫岁月 2024-11-13 02:54:49

很简单,只需添加这段代码即可

 private Handler threadHandler = new Handler() 
{
    public void handleMessage(android.os.Message msg) 
    {
        sec=time2++;
        if(sec == 59)
        {
            time2=0;
            sec = time2++;
            min=time1++;
            if(sec< 10)
              sec= "0"+sec

        }
        if(min == 59)
        {
            time1=0;
            min = time1++;
            hr=time3++;
           if(min< 10)
               min= "0"+min
        }   

        if(hr< 10)
           hr= "0"+hr          
        stopWatch.setText(""+hr+":"+min+":"+sec);
    }
};

It's simple you just add this code

 private Handler threadHandler = new Handler() 
{
    public void handleMessage(android.os.Message msg) 
    {
        sec=time2++;
        if(sec == 59)
        {
            time2=0;
            sec = time2++;
            min=time1++;
            if(sec< 10)
              sec= "0"+sec

        }
        if(min == 59)
        {
            time1=0;
            min = time1++;
            hr=time3++;
           if(min< 10)
               min= "0"+min
        }   

        if(hr< 10)
           hr= "0"+hr          
        stopWatch.setText(""+hr+":"+min+":"+sec);
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文