Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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处理程序_Android_Loops_Android Handler - Fatal编程技术网

如何在循环中使用android处理程序

如何在循环中使用android处理程序,android,loops,android-handler,Android,Loops,Android Handler,我正在构建我的第一个android应用程序,我正在尝试制作一个记忆游戏。无论如何,我需要让一组按钮改变颜色1秒,然后按顺序返回其原始颜色,例如:button1变为黄色,保持不变1秒,然后返回灰色,button2变为黄色1秒,然后返回,依此类推。我尝试使用处理程序,但它总是在最后一次迭代后才起作用,这是我的代码: for (i = 0; i < 9; i++) { buttonList.get(i).setBackgroundColor(Color.

我正在构建我的第一个android应用程序,我正在尝试制作一个记忆游戏。无论如何,我需要让一组按钮改变颜色1秒,然后按顺序返回其原始颜色,例如:button1变为黄色,保持不变1秒,然后返回灰色,button2变为黄色1秒,然后返回,依此类推。我尝试使用处理程序,但它总是在最后一次迭代后才起作用,这是我的代码:

for (i = 0; i < 9; i++) {

                    buttonList.get(i).setBackgroundColor(Color.YELLOW);


                    runnable =new Runnable(){
                        @Override
                        public void run() {

                             buttonList.get(i).setBackgroundColor(Color.GRAY);

                        }
                    };
                    handler.postDelayed(runnable,1000);}
(i=0;i<9;i++)的
{
buttonList.get(i).setBackgroundColor(颜色.黄色);
runnable=新的runnable(){
@凌驾
公开募捐{
buttonList.get(i).setBackgroundColor(颜色.灰色);
}
};
handler.postDelayed(runnable,1000);}
我做错了什么

编辑
找到了方法。首先,我需要创建一个runnable类,该类在MyRunnable实现runnable后使用paramaters(使用runnable接口),然后编写一个使用该paramater的方法,我不能使用常规类,因为它依赖于I,I会随着迭代而改变

您需要在每个循环中创建一个新的Runnable,因为所有9个延迟帖子都运行与您在第9个循环(即最后一个循环)中创建的Runnable相同的Runnable,因为循环完成所需的时间无疑不到一秒钟。因此,请尝试以下方法:

for (i = 0; i < 9; i++) {
    buttonList.get(i).setBackgroundColor(Color.YELLOW);
    Runnable runnable = new Runnable(){
         @Override
         public void run() {
             buttonList.get(i).setBackgroundColor(Color.GRAY);
         }};
    handler.postDelayed(runnable,1000);
 }
(i=0;i<9;i++)的
{
buttonList.get(i).setBackgroundColor(颜色.黄色);
Runnable Runnable=新的Runnable(){
@凌驾
公开募捐{
buttonList.get(i).setBackgroundColor(颜色.灰色);
}};
handler.postDelayed(可运行,1000);
}

您需要在每个循环中创建一个新的Runnable,因为所有9个延迟的POST都运行与您在第9个也是最后一个循环中创建的Runnable相同的Runnable,因为循环无疑需要不到一秒钟的时间来完成。因此,请尝试以下方法:

for (i = 0; i < 9; i++) {
    buttonList.get(i).setBackgroundColor(Color.YELLOW);
    Runnable runnable = new Runnable(){
         @Override
         public void run() {
             buttonList.get(i).setBackgroundColor(Color.GRAY);
         }};
    handler.postDelayed(runnable,1000);
 }
(i=0;i<9;i++)的
{
buttonList.get(i).setBackgroundColor(颜色.黄色);
Runnable Runnable=新的Runnable(){
@凌驾
公开募捐{
buttonList.get(i).setBackgroundColor(颜色.灰色);
}};
handler.postDelayed(可运行,1000);
}
您正在同步(同时)将所有按钮的颜色设置为黄色,并创建9个异步任务(每个按钮一个)以在一秒钟后将颜色更改为灰色。这意味着所有按钮将在大约1秒后(或多或少)同时将颜色变回灰色

将处理程序视为向其添加任务的队列。调用
postDelayed()
正在计划将来要执行的任务,但所有任务都是在同一时间计划的,因此将来所有任务都将在同一时间执行

我还没有运行它,但我认为这种方法更符合您的要求:

// Those are fields
private int buttonIndex = 0;
private boolean yellow = false;
private final Handler handler = new Handler(new Handler.Callback() {
    @Override
    public void handleMessage(Message msg) {
        if (!yellow) {
            buttonList.get(buttonIndex).setBackgroundColor(Color.YELLOW);
            handler.sendEmptyMessageDelayed(0, 1000);
        } else {
            buttonList.get(buttonIndex).setBackgroundColor(Color.GRAY);
            if (++buttonIndex < 9) handler.sendEmptyMessage(0);
        }
        yellow = !yellow;
}});

// Call this to start the sequence.
handler.sendEmptyMessage(0);
//这些是字段
私有int按钮索引=0;
私有布尔黄=假;
private final Handler=new Handler(new Handler.Callback()){
@凌驾
公共无效handleMessage(消息消息消息){
如果(!黄色){
buttonList.get(buttonIndex).setBackgroundColor(Color.YELLOW);
handler.sendEmptyMessageDelayed(0,1000);
}否则{
buttonList.get(buttonIndex).setBackgroundColor(Color.GRAY);
if(++buttonIndex<9)handler.sendEmptyMessage(0);
}
黄色=!黄色;
}});
//调用此命令以开始序列。
handler.sendEmptyMessage(0);
请注意,我使用的是
sendEmptyMessage*()
而不是
post*()
,但这两种方法都可以使用。此外,处理程序的消息(任务)可以有输入参数,因此最好使用它们。

您正在同步(同时)将所有按钮的颜色设置为黄色,并创建9个异步任务(每个按钮一个),以便在一秒钟后将颜色更改为灰色。这意味着所有按钮将在大约1秒后(或多或少)同时将颜色变回灰色

将处理程序视为向其添加任务的队列。调用
postDelayed()
正在计划将来要执行的任务,但所有任务都是在同一时间计划的,因此将来所有任务都将在同一时间执行

我还没有运行它,但我认为这种方法更符合您的要求:

// Those are fields
private int buttonIndex = 0;
private boolean yellow = false;
private final Handler handler = new Handler(new Handler.Callback() {
    @Override
    public void handleMessage(Message msg) {
        if (!yellow) {
            buttonList.get(buttonIndex).setBackgroundColor(Color.YELLOW);
            handler.sendEmptyMessageDelayed(0, 1000);
        } else {
            buttonList.get(buttonIndex).setBackgroundColor(Color.GRAY);
            if (++buttonIndex < 9) handler.sendEmptyMessage(0);
        }
        yellow = !yellow;
}});

// Call this to start the sequence.
handler.sendEmptyMessage(0);
//这些是字段
私有int按钮索引=0;
私有布尔黄=假;
private final Handler=new Handler(new Handler.Callback()){
@凌驾
公共无效handleMessage(消息消息消息){
如果(!黄色){
buttonList.get(buttonIndex).setBackgroundColor(Color.YELLOW);
handler.sendEmptyMessageDelayed(0,1000);
}否则{
buttonList.get(buttonIndex).setBackgroundColor(Color.GRAY);
if(++buttonIndex<9)handler.sendEmptyMessage(0);
}
黄色=!黄色;
}});
//调用此命令以开始序列。
handler.sendEmptyMessage(0);

请注意,我使用的是
sendEmptyMessage*()
而不是
post*()
,但这两种方法都可以使用。此外,处理程序的消息(任务)可以有输入参数,因此最好使用它们。

因为十次迭代完成的速度超过1秒因为十次迭代完成的速度超过1秒感谢您的回复,它仍然会说不幸。感谢您的回复,它仍然会说不幸。感谢您的回复,但它仍然如此。我认为我不能使用I,需要使用某种方式将其值传递给runnable,而不是它本身,但我不知道如何传递。谢谢您的回复,但它仍然会这样做。我认为我不能使用I,需要使用某种方式将其值传递给runnable,而不是它本身,但我不知道如何传递