按钮设置超时(延迟一秒)



我需要帮助,因为我正在Android Studio中创建一个简单的程序。有两种不同的背景图像。我想要的只是按下一个按钮,让我的背景图像更改为下一个图像,有 1 秒的延迟,然后变回原始图像。

我能够在按压机上更改背景图像,但是我不知道如何添加延迟并更改回背景图像。

编辑:谢谢你们帮助我。我对这个东西很陌生,我花了很多时间试图自己弄清楚。终于让它工作了!

在这种情况下,您需要Handler..

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Change the image back here
}
}, 1000); // 1 sec delay

这可能会对您有所帮助。

// set the background here
compositeDisposable.add(
Completable.timer(1, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(() -> {
//update the background
}, Throwable::printStackTrace));

最新更新