当我改变ImageButton图像时,不会实时更改

时间:2013-12-27 21:23:00

标签: java android sleep imagebutton

我将此代码用于更改ImageButton图片,但不会实时更改。

它仅更改一次,而不是ImageButton ImageButton

for(int i = 1 ; i <= left_num ; i++ ){
    int resourceId = this.getResources().getIdentifier("c"+i+"_r"+right_num, "id", this.getPackageName());
    ImageButton imageButton = (ImageButton) findViewById(resourceId);
    imageButton.setBackgroundResource(R.drawable.white_circle);
    sleep(500);
}

1 个答案:

答案 0 :(得分:1)

它可能会“实时”更改,但您无法看到它,因为代码行sleep(500)会被忽略,并且不会按照您的预期执行。

修改

您可以通过调用以下方法将Timer设置为按顺序更新ImageButtons:

private void updateImageButtonsSequentially() {
    int iteration = 0;
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
              int resourceId = this.getResources().getIdentifier("c"+iteration+"_r"+right_num, "id", this.getPackageName());
              iteration++;
              ImageButton imageButton = (ImageButton) findViewById(resourceId);
              imageButton.setBackgroundResource(R.drawable.white_circle);
        }
    },0,500);
}