Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在同一个android活动中使用两个Thread.sleep()_Android_Android Thread - Fatal编程技术网

在同一个android活动中使用两个Thread.sleep()

在同一个android活动中使用两个Thread.sleep(),android,android-thread,Android,Android Thread,也许有人能告诉我我做错了什么,我正在编写一个Android应用程序,它将以一个时间间隔显示3个视图,所以基本思想是 ViewOne.setVisibility(View.INVISIBLE)//这是多余的,但为了清楚起见,我把它放在这里 ViewTwo.setVisibility(View.INVISIBLE); ViewThree.setVisibility(视图.不可见); ViewOne.setVisibility(View.VISIBLE); //这应该是1秒的停车。 试一试{ 睡眠(1

也许有人能告诉我我做错了什么,我正在编写一个Android应用程序,它将以一个时间间隔显示3个视图,所以基本思想是

ViewOne.setVisibility(View.INVISIBLE)//这是多余的,但为了清楚起见,我把它放在这里
ViewTwo.setVisibility(View.INVISIBLE);
ViewThree.setVisibility(视图.不可见);
ViewOne.setVisibility(View.VISIBLE);
//这应该是1秒的停车。
试一试{
睡眠(1000);
}捕捉(中断异常e){
e、 printStackTrace();
}
ViewTwo.setVisibility(View.VISIBLE);
//这应该再停1秒钟。
试一试{
睡眠(1000);
}捕捉(中断异常e){
e、 printStackTrace();
}
ViewThree.setVisibility(View.VISIBLE);

但是,该活动不是单独停止1000毫秒,而是等待2000毫秒开始,然后同时显示所有视图。我是android和java开发的新手,如果我做了一件蠢事,我很抱歉。提前感谢大家。

根本原因:您的代码阻止了UI线程,因此在线程空闲(或未被阻止)之前,它不会更新UI。

解决方案:我给你这个解决方案是为了满足你的需要

final Handler handler = new Handler();

ViewOne.setVisibility(View.INVISIBLE); //This is redundant but I put here for clarity
ViewTwo.setVisibility(View.INVISIBLE);
ViewThree.setVisibility(View.INVISIBLE);

ViewOne.setVisibility(View.VISIBLE);
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        ViewTwo.setVisibility(View.VISIBLE);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                ViewThree.setVisibility(View.VISIBLE);
            }
        }, 1000);
    }
}, 1000);
确保在变量
ViewTwo
viewtwree
之前添加
final
关键字

更新:我不知道您的逻辑代码或您的意图,但如果您想重复N次

int secondsForEachStep = 3;
for (int i = 0; i < 3; i++) {
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            wordText.setVisibility(View.INVISIBLE);
            myImageView.setVisibility(View.INVISIBLE);
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    wordText.setVisibility(View.VISIBLE);
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            myImageView.setVisibility(View.VISIBLE);
                        }
                    }, 1000);
                }
            }, 1000);

        }
    }, (i * secondsForEachStep + 1) * 1000);
}
对于每个步骤,我们需要3秒钟才能完成,从中我们可以发现规则是

i * secondsForEachStep + 1

签出这个:谢谢我的朋友,这个代码使所有的工作。我唯一不明白的是最后一部分(I*repeatTime+1)*1000);。。这段代码的目的是什么?这使得所有的工作和它的所有干净的解释,非常感谢。
i * secondsForEachStep + 1