如何以特定的时间延迟频繁更新文本视图?

How to update a textview frequently with a spesific time delay?

我需要在 android 工作室中以特定的时间延迟经常更新 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);
}

试试下面的代码。这样就可以了。如果您发现任何问题,请告诉我。

 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();
}
  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 毫秒如你所愿

  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);
}

最简单的方法。这里 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()

上停止处理程序