求你原谅我是一个新的开发者。
我正在使用SystemClock.uptimeMillis
inside ofRunnable
使它看起来像秒表。我从谷歌拿了秒表源代码。之后,我每 30 秒进行一次修改,变量(命名为价格)会增加。
奇怪的是,当我重新开始秒表时,价格不会回到初始值。我试过重置价格值,onActivityResult
但秒表显示的是随机数。如何让价格变量回到初始值?
这是我的代码:
Handler handler = new Handler();
long startTime = 0L, timeinMilliseconds=0L,timeSwapBuff=0L, updateTime=0L;
int pressCounter = 0;
int price = 3000;
Runnable updateTimerThread = new Runnable() {
@SuppressLint({"DefaultLocale", "SetTextI18n"})
@Override
public void run() {
timeinMilliseconds = SystemClock.uptimeMillis()-startTime;
updateTime = timeSwapBuff+timeinMilliseconds;
int secs = (int)(updateTime/1000);
int mins = secs/60;
int hours = mins/60;
secs%=60;
int milliseconds = (int)(updateTime%1000);
tv_timer.setText(String.format("%2d",hours)+":"+String.format("%2d",mins)+":"+String.format("%2d",secs)+":"
+String.format("%3d",milliseconds));
if (secs % 30 == 0){
price++;
tv_biaya.setText("Rp. "+price+",00");
}
handler.postDelayed(this,0);
}
};
当按钮单击该方法时触发此代码
startTime = SystemClock.uptimeMillis();
handler.postDelayed(updateTimerThread,0);