按钮顺序可见

时间:2016-07-07 18:08:33

标签: android xml button

我试图在按钮按下时执行一种停车灯功能,但是每种颜色都有两种颜色。所需的功能是红色,红色,黄色,黄色,绿色和绿色。我试图在每种颜色之间设置延迟,但它没有顺序发生它只是加起来延迟并且它们都立刻显示出来。

以下是我尝试过的活动摘录:

Button previewButton = (Button) findViewById(R.id.preview_visual_button);
        final Button redButton1 = (Button) findViewById(R.id.red_button_1);
        final Button redButton2 = (Button) findViewById(R.id.red_button_2);
        final Button yellowButton1 = (Button) findViewById(R.id.yellow_button_1);
        final Button yellowButton2 = (Button) findViewById(R.id.yellow_button_2);
        final Button greenButton1 = (Button) findViewById(R.id.green_button_1);
        final Button greenButton2 = (Button) findViewById(R.id.green_button_2);

        redButton1.setVisibility(View.GONE);
        redButton2.setVisibility(View.GONE);
        yellowButton1.setVisibility(View.GONE);
        yellowButton2.setVisibility(View.GONE);
        greenButton1.setVisibility(View.GONE);
        greenButton2.setVisibility(View.GONE);

        if(previewButton != null) {
            previewButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int time = 100;
                    delay(time);
                }
            });
        }
    }

    public void delay(final int c){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(c);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Button redButton1 = (Button) findViewById(R.id.red_button_1);
                Button redButton2 = (Button) findViewById(R.id.red_button_2);
                Button yellowButton1 = (Button) findViewById(R.id.yellow_button_1);
                Button yellowButton2 = (Button) findViewById(R.id.yellow_button_2);
                Button greenButton1 = (Button) findViewById(R.id.green_button_1);
                Button greenButton2 = (Button) findViewById(R.id.green_button_2);

                redButton1.setVisibility(View.VISIBLE);
                redButton2.setVisibility(View.VISIBLE);
                yellowButton1.setVisibility(View.VISIBLE);
                yellowButton2.setVisibility(View.VISIBLE);
                greenButton1.setVisibility(View.VISIBLE);
                greenButton2.setVisibility(View.VISIBLE);
            }
        }, c);

    }

编辑:

previewButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int time = 1000;
                    redButton1.setVisibility(View.VISIBLE);
                    delay(time);
                    redButton2.setVisibility(View.VISIBLE);
                    delay(time);
                    yellowButton1.setVisibility(View.VISIBLE);
                    delay(time);
                    yellowButton2.setVisibility(View.VISIBLE);
                    delay(time);
                    greenButton1.setVisibility(View.VISIBLE);
                    delay(time);
                    greenButton2.setVisibility(View.VISIBLE);
                }   
            });
    }

    public void delay(final int time) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

2 个答案:

答案 0 :(得分:0)

因为这基本上是postDelayed方法的作用。它在指定时间后运行runnable中的代码。所以它延迟了100ms然后运行只是打开所有按钮。另外,我不确定你在UI线程上调用Thread.sleep()会尝试做什么。我建议编写一个新方法,只在一个单独的线程中运行Thread.sleep()并在每对按钮切换命令之间调用它。有点像这样

public void pause(int time) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();
 }

然后基本上在每对按钮切换语句之间调用它。这应该可以解决问题。

答案 1 :(得分:0)

Update UI with Thread sleep

如果将来有人访问,这将有助于回答这个问题。