本文介绍了如何在Android中延迟秒数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想延迟几秒钟并显示Toast,我尝试 SystemClock.sleep

I want to delay seconds and show Toast,I try to SystemClock.sleep

但它只显示最后一条消息( 10s)...

But it only show last message("10s")...

Toast.makeText(MainActivity.this,"1s", Toast.LENGTH_SHORT).show();
SystemClock.sleep(5000);
Toast.makeText(MainActivity.this,"5s", Toast.LENGTH_SHORT).show();
SystemClock.sleep(5000);
Toast.makeText(MainActivity.this,"10s", Toast.LENGTH_SHORT).show();

应该按顺序显示1s,5s,10s吗?

That should be displayed in sequence 1s, 5s, 10s is not it?

我也参考了这种做法,但是无法实现...

I also made reference to this practice, but it can not be achieved...How to set delay in android?

那么问题出在哪里?

推荐答案

尝试处理程序

public void showToast(final String message, int timeInMilliSeconds, final Context context) {
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
        }
    };
    Handler handler = new Handler();
    handler.postDelayed(runnable, timeInMilliSeconds);
}

用法:

showToast("1s, 1000, this);
showToast("5s, 5000, this);
showToast("10s, 10000, this);

这篇关于如何在Android中延迟秒数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:31
查看更多