处理程序postDelayed-每秒打印一些内容

maf*_*oso 4 video android handler android-camera2

我想使用来打印当前秒handler。我精确地录制了视频,10 seconds并且想要设置TextView每秒的文本。

录制10秒的工作原理如下:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
           stopRecordingVideo();
    }
}, 11000);  // don't know why 11000 but it only works this way
Run Code Online (Sandbox Code Playgroud)

10秒钟后,该方法stopRecordingVideo()将执行。那么,如何每秒更改TextView的文本呢?

maf*_*oso 5

工作答案:

int t = 0;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        t++;
        textView.setText(getString(R.string.formatted_time, t));
        if(t<10) {
            handler.postDelayed(this, 1000);
        }
    }
}, 1000);
Run Code Online (Sandbox Code Playgroud)

其中formatted_time是这样的:

<string android:name="formatted_time">%d seconds</string>

  • 我必须将 t 声明为最终的 int[],但在 tis 更改之后它就起作用了。谢谢! (2认同)