如何在不使用线程的情况下延迟android的循环。

时间:2021-05-03 21:00:37

I wanted to delay a for loop without using Thread.sleep because that method make my whole application hang. I tried to use handler but it doesn't seems to work inside a loop. Can someone please point out the mistake in my code.

我希望在不使用线程的情况下延迟for循环。因为这个方法使我的整个应用程序挂起了。我尝试使用处理程序,但它似乎没有在循环中工作。有人能指出我代码中的错误吗?

public void onClick(View v) { 
    if (v == start)
    {   
        for (int a = 0; a<4 ;a++) {

         Handler handler1 = new Handler();
         handler1.postDelayed(new Runnable() {

        ImageButton[] all= {btn1, btn2, btn3, btn4};
        btn5 = all[random.nextInt(all.length)];
        btn5.setBackgroundColor(Color.RED);

             @Override
             public void run() {

             }
             }, 1000);
        } 
        }
     }

Basically what I wanted to do is that I got 4 ImageButton and I change each of their background to red by using a loop in order. Thats why I need a delay inside my loop, if not all the ImageButton will just directly turn red without showing which ImageButton turn first.

基本上,我想做的是,我有4个ImageButton,我用一个循环来改变它们的每个背景。这就是为什么我需要在我的循环中有一个延迟,如果不是所有的ImageButton都将直接变成红色,而不显示哪个ImageButton先转。

4 个解决方案

#1


30  

Your for loop should be:

你的for循环应该是:

final ImageButton[] all= {btn1, btn2, btn3, btn4};
Handler handler1 = new Handler();
for (int a = 1; a<=all.length ;a++) {
    handler1.postDelayed(new Runnable() {

         @Override
         public void run() {
              ImageButton btn5 = all[random.nextInt(all.length)];
              btn5.setBackgroundColor(Color.RED);
         }
         }, 1000 * a);
    } 
}

This way it achieves your desired behavior of staggering the color change.

这样就能达到你想要的改变颜色的行为。

Edited for syntax

编辑的语法

#2


11  

You can use a Handler instead of for loop. You should not call Thread.sleep() on the UI thread.

您可以使用处理程序而不是for循环。您不应该在UI线程上调用thread. sleep()。

final Handler handler = new Handler();
final Runnable runnable = new Runnable() { 
    @Override
    public void run() {
        // do something
        handler.postDelayed(runnable, 1000L);  // 1 second delay
    }
};
runnable.run();

#3


3  

The following code doing the task in every second:

下面的代码每一秒都在执行任务:

final Handler handler = new Handler();
Runnable task = new Runnable() {
    @Override
    public void run() {
        Log.d(TAG, "Doing task");
        handler.postDelayed(this, 1000);
    }
};
handler.post(task); 

#4


1  

Try this :

试试这个:

public void onClick(View v) { 
    if (v == start) {   
        for (int a = 0; a<4 ;a++) {
            Handler handler1 = new Handler();
            handler1.postDelayed(new Runnable() {
                ImageButton[] all= {btn1, btn2, btn3, btn4};
                @Override
                public void run() {
                    btn5 = all[random.nextInt(all.length)];
                    btn5.setBackgroundColor(Color.RED);
                }
            }, 1000);
        } 
    }
}

Example for Delay :

延迟的例子:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        // Do something after 5s = 5000ms
        buttons[inew][jnew].setBackgroundColor(Color.Red);
    }
}, 5000);

#1


30  

Your for loop should be:

你的for循环应该是:

final ImageButton[] all= {btn1, btn2, btn3, btn4};
Handler handler1 = new Handler();
for (int a = 1; a<=all.length ;a++) {
    handler1.postDelayed(new Runnable() {

         @Override
         public void run() {
              ImageButton btn5 = all[random.nextInt(all.length)];
              btn5.setBackgroundColor(Color.RED);
         }
         }, 1000 * a);
    } 
}

This way it achieves your desired behavior of staggering the color change.

这样就能达到你想要的改变颜色的行为。

Edited for syntax

编辑的语法

#2


11  

You can use a Handler instead of for loop. You should not call Thread.sleep() on the UI thread.

您可以使用处理程序而不是for循环。您不应该在UI线程上调用thread. sleep()。

final Handler handler = new Handler();
final Runnable runnable = new Runnable() { 
    @Override
    public void run() {
        // do something
        handler.postDelayed(runnable, 1000L);  // 1 second delay
    }
};
runnable.run();

#3


3  

The following code doing the task in every second:

下面的代码每一秒都在执行任务:

final Handler handler = new Handler();
Runnable task = new Runnable() {
    @Override
    public void run() {
        Log.d(TAG, "Doing task");
        handler.postDelayed(this, 1000);
    }
};
handler.post(task); 

#4


1  

Try this :

试试这个:

public void onClick(View v) { 
    if (v == start) {   
        for (int a = 0; a<4 ;a++) {
            Handler handler1 = new Handler();
            handler1.postDelayed(new Runnable() {
                ImageButton[] all= {btn1, btn2, btn3, btn4};
                @Override
                public void run() {
                    btn5 = all[random.nextInt(all.length)];
                    btn5.setBackgroundColor(Color.RED);
                }
            }, 1000);
        } 
    }
}

Example for Delay :

延迟的例子:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        // Do something after 5s = 5000ms
        buttons[inew][jnew].setBackgroundColor(Color.Red);
    }
}, 5000);