在Android的秒表问题 [英] problem with stopWatch in android
问题描述
在我的应用我已经把秒表两个按钮。秒表工作在一个适当的方式。但问题是在启动定时器看上去0:0:0,当它开始计数个位数都被改变了两位数为0点12分53秒
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.
这会影响和干扰其他层了。在启动本身我希望它显示为00:00:00在默认情况下,所以我不能让布局的变化。
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.
但我不知道从哪里得到的价值这一点。以下是我的code
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............
推荐答案
这应该做的工作:
String time = String.format("%02d:%02d:%02d", hr, min, sec);
stopWatch.setText(time);
或者在单线版本
stopWatch.setText(String.format("%02d:%02d:%02d", hr, min, sec));
这篇关于在Android的秒表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!