-1

求你原谅我是一个新的开发者。

我正在使用SystemClock.uptimeMillisinside 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);
4

1 回答 1

0

由于“价格 % 30 == 0”的逻辑,价格似乎仍在上涨。我的错。当秒表秒转为 0 时,%30 也为 0。因此,它不会进入初始值。Runnable我已经改变了逻辑,我为价格创造了一个新的。

于 2018-03-07T08:04:27.773 回答