如何频繁地更新文本视图且延迟很长时间?

时间:2018-11-07 06:55:41

标签: android loops handler

我需要在Android Studio中以特定的时间延迟频繁更新TextView。代码如下。谢谢。

编辑:我还需要通过单击按钮或使用“ if”控件结束循环。

//INFLATION CALCULATION !!!
/**
 * This method calculates Inflation value.
 */
public void calculateInflation() {
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            inflation = (cpi-cpiIni)/cpiIni*100;
            displayInflation();
            cpiIni = cpi;
        }
    }, delay*12);
}

4 个答案:

答案 0 :(得分:0)

尝试以下代码。这将达到目的。如果您发现任何问题,请告诉我。

 public void calculateInflation() {
    mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        inflation = (cpi-cpiIni)/cpiIni*100;
        displayInflation();
        cpiIni = cpi;
        if(shouldRepeat)
           calculateInflation();
        }
    }, delay*12);
}

第二种方法可以是CountDownTimer。制作如下代码所示的方法

 public void timerTask(final int loopTime){
     //Loop time is the actual time for repeatation
    new CountDownTimer(loopTime, 1000) {
        public void onTick(long millisUntilFinished) {
            //this tells you one second is passed
        }

        public void onFinish() {
            //here on time finish you need to define your task
            inflation = (cpi-cpiIni)/cpiIni*100;
            displayInflation();
            cpiIni = cpi;
            //call the same method again for looping
            timerTask(loopTime);
        }
    }.start();
}

答案 1 :(得分:0)

  private Runnable updateTimerThread = new Runnable() {

    public void run() {

        inflation = (cpi-cpiIni)/cpiIni*100;
        displayInflation();
        cpiIni = cpi;

        customHandler.postDelayed(this, 0);
    }

};


  public void startTimer() {
    //timer
    startTime = SystemClock.uptimeMillis();
    customHandler.postDelayed(updateTimerThread, 0);

}

public void stopTimer() {
    //timer stops
    customHandler.removeCallbacks(updateTimerThread);
    //timer ends
}

作为可运行线程的引用,使用startTimer()启动它,并使用stopTimer()删除线程,就像您在特定条件下单击按钮或单击按钮时所说的那样。此外,您还可以将postDelayed毫秒更改为ur希望

答案 2 :(得分:0)

  1. 在runnable内部调用相同的方法,以使循环继续进行
  2. 使用一个标志以便能够停止循环:shouldCalculate

private boolean shouldCalculate = true; // set to false when you want to end the loop

public void calculateInflation() {
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (shouldCalculate) {
                inflation = (cpi-cpiIni)/cpiIni*100;
                displayInflation();
                cpiIni = cpi;
                calculateInflation();
            }
        }
    }, delay*12);
}

答案 3 :(得分:0)

最简单的方法。 updateRunnable在这里自称延迟。将updateRunnable设置为全局变量以从任何地方访问。

Runnable updateRunnable = new Runnable() {
    @Override
    public void run() {

        inflation = (cpi-cpiIni)/cpiIni*100;
        displayInflation();
        cpiIni = cpi;

        handler.postDelayed(this, UPDATE_TIME);
    }
};

启动处理程序。在这里,我们立即启动处理程序,没有延迟。

handler.postDelayed(updateRunnable, 0)

停止处理程序

handler.removeCallbacks(updateRunnable)

通过这种方式,别忘了在onDestroy()上停止处理程序