1

我的示例应用程序中有两个按钮,我想以 4 秒的延迟一个接一个地更改按钮的背景图像,到目前为止我已经开发了代码,你可以在下面看到它,但是它让我的应用程序进入睡眠状态并显示没有。

请有人帮助我,我可以这样做。

   int m =0;
    int delay = 4;  //Seconds

    while(m < 4)
    {



     // int i = (int) (Math.random() * num + 1);

    if(m==0)
    {
        button1.postDelayed(new Runnable()
        {
            public void run()
            {
            // TODO Auto-generated method stub
            button1.setBackgroundResource(R.drawable.buttonyellow);
            m++;
            }
        }, 1000*delay);

    }


    else if(m==1)
    {
        button2.postDelayed(new Runnable()
        {
            public void run()
            {
                // TODO Auto-generated method stub
                button2.setBackgroundResource(R.drawable.buttonyellow);
                m++;
            }
        }, 1000*delay);

    }





    if(m==2)
    {
        button1.postDelayed(new Runnable()
        {
            public void run()
            {
            // TODO Auto-generated method stub
            button1.setBackgroundResource(R.drawable.buttonblue);
            m++;
            }
        }, 1000*delay);

    }


    else if(m==3)
    {
        button2.postDelayed(new Runnable()
        {
            public void run()
            {
                // TODO Auto-generated method stub
                button2.setBackgroundResource(R.drawable.buttonblue);
                m++;
            }
        }, 1000*delay);

    }



    }
4

3 回答 3

1

postDelayed不会延迟当前线程的执行,因此您的线程会m == 0反复发布案例。

您可能希望您发布的 Runnable 调用一些方法,该方法不仅设置背景并增加数字,而且再次调用此逻辑(以选择新的 Runnable 发布)。

例如(警告:未经测试的代码)

// assuming button1 and button2 are available
new Runnable() {
  private int m = 0;
  private final int delay = 4000;

  @Override
  public void run() {
    if (m == 0) {
      button1.setBackgroundResource(R.drawable.buttonyellow);
      button1.postDelayed(this, delay);
    } else if (m == 1) {
      button2.setBackgroundResource(R.drawable.buttonyellow);
      button2.postDelayed(this, delay);
    }
    // more cases here -- also consider making m wrap around
    m++;
  }
}.run();

自然这应该在 UI 线程上调用(如果它不在 UI 线程上调用,它应该被分派到 UI 线程post或类似)。

于 2012-09-28T14:50:21.300 回答
1
int mark = 0; 

button2.postDelayed(new Runnable()
    {
        public void run()
        {
            switch(mark++){
                 case 0:button2.setBackgroundResource(R.drawable.buttonyellow); break;
                 case 1:button2.setBackgroundResource(R.drawable.buttongreen); break;
                 ..ect
            }
            if(mark==Max)mark=0;

         button2.postDelayed(this,4000);
        }
    }, 4000);

请注意,这将持续到时间结束,或者当您的应用停止时。

于 2012-09-28T14:53:53.153 回答
-1

您的 while 循环导致您的 UI 线程等待这些操作完成。您最好的两个选择是在新线程中调用它:

new Runnable() {
    @Override
    public void run() {
        //Your code
    }
}.run();

或者将它们嵌入链中:

button1.postDelayed(new Runnable()
    {
        public void run()
        {
            button1.setBackgroundResource(R.drawable.buttonyellow);
            button2.postDelayed(new Runnable()
            {
                public void run()
                {
                    button2.setBackgroundResource(R.drawable.buttonyellow);
                    button1.postDelayed(new Runnable()
                    {
                        public void run()
                        {
                            button1.setBackgroundResource(R.drawable.buttonblue);
                            button2.postDelayed(new Runnable()
                            {
                                public void run()
                                {
                                    button2.setBackgroundResource(R.drawable.buttonblue);
                                }
                            }, 1000*delay);
                         }
                    }, 1000*delay);
                }
            }, 1000*delay);
        }
    }, 1000*delay);
于 2012-09-28T14:53:06.767 回答