这是我的代码:
public class BFragment extends SherlockFragment {
private Button start;
private View v;
private Button reset;
private Button pause;
private TextView hourTextView;
private TextView minTextView;
private TextView secTextView;
private int secondCounter;
private int minuteCounter;
private int hourCounter;
private Thread timerThread;
private boolean continueThread = false;
private boolean isRunning = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.activity_bfragment, container, false);
start = (Button) v.findViewById(R.id.start);
pause = (Button) v.findViewById(R.id.pause);
reset = (Button) v.findViewById(R.id.reset);
hourTextView = (TextView) v.findViewById(R.id.hh);
minTextView = (TextView) v.findViewById(R.id.mm);
secTextView = (TextView) v.findViewById(R.id.ss);
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (isRunning) {
} else {
continueThread = true;
timeUpdate();
isRunning = true;
isRunning = true;
}
}
});
pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
continueThread = false;
isRunning = false;
start.setText("Resume");
isRunning = false;
}
});
reset.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
hourTextView.setText("00");
minTextView.setText("00");
secTextView.setText("00");
hourCounter = 0;
secondCounter = 0;
minuteCounter = 0;
continueThread = false;
isRunning = false;
}
});
return v;
}
final Handler mHandlerUpdateSec = new Handler();
final Runnable mUpdateSec = new Runnable() {
public void run() {
String temp = "" + secondCounter;
System.out.println("Temp second counter length: " + temp.length());
if(temp.length() == 1)
secTextView.setText("0" + secondCounter);
else
secTextView.setText("" + secondCounter);
}
};
final Handler mHandlerUpdateMinute = new Handler();
final Runnable mUpdateMinute= new Runnable() {
public void run() {
String temp = "" + minuteCounter;
System.out.println("Temp second counter length: " + temp.length());
if(temp.length() == 1)
minTextView.setText("0" + minuteCounter);
else
minTextView.setText("" + minuteCounter);
}
};
final Handler mHandlerUpdateHour = new Handler();
final Runnable mUpdateHour = new Runnable() {
public void run() {
String temp = "" + hourCounter;
System.out.println("Temp second counter length: " + temp.length());
if(temp.length() == 1)
hourTextView.setText("0" + hourCounter);
else
hourTextView.setText("" + hourCounter);
}
};
public void timeUpdate()
{
timerThread = new Thread(new Runnable() {
public void run() {
while(continueThread){
Date newDate = new Date();
Date date = new Date();
System.out.println(newDate.getTime() - date.getTime());
if(true){
secondCounter = secondCounter+1;
mHandlerUpdateSec.post(mUpdateSec);
System.out.println("Inside the Theread ..."+secondCounter);
if(secondCounter > 59){
minuteCounter = minuteCounter + 1;
mHandlerUpdateMinute.post(mUpdateMinute);
secondCounter = 0;
if(minuteCounter > 59){
hourCounter = hourCounter + 1;
mHandlerUpdateHour.post(mUpdateHour);
minuteCounter = 0;
}
}
}
}
});
timerThread.start();
}
}
如果用户真的快速按下开始/暂停(几乎在一起),那么计时器会快速计数并跳过秒......它会一直停止直到按下暂停然后再次正常工作?为什么这样以及如何预防?
谢谢!
答案 0 :(得分:0)
对于初学者来说,代码中没有任何延迟。你的timeUpdate()
基本上是这样做的:
while(continueThread){
secondCounter = secondCounter+1;
// ... display the counter ...
}
您需要做一些事情来将您的应用与实时时钟联系起来。一种简单的方法是计算每次通过循环所经过的时间并显示出来。一个简单的优化是在代码中加入一些检查以查看是否有变化。为了更高效,您可以让线程休眠而不是spinning。
我想建议,不是每次计数器改变时都专注于重新绘制显示,而是转过身来。等待并做任何事情,直到用人的话来说,这是用户查看更新的时间。 (这可能是每秒,每10秒,无论如何。)当是时候向用户显示新的更新,然后查看时间戳,测量经过的时间,并将其写入显示器。