动态改变按钮问题的颜色

时间:2017-06-18 05:54:29

标签: android handler runnable

我正在尝试更改编程中的按钮颜色, 如果只有一个按钮代码工作正常,但如果有四个按钮,则多个颜色会改变颜色

在我的代码中,我随机选择一个四分之一的按钮并更改其颜色, 但实际上,不止一种变色,有时是2种,有时是3种,有时是3种(有时候它们都在改变颜色,但我希望它们逐一改变颜色)。

以下是我的代码

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button b1, b2, b3, b4;
    Random r = new Random();
    int random_selection;
    Thread t1;
int i;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.button);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        b4 = (Button) findViewById(R.id.button4);

        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);
        b4.setOnClickListener(this);


 main();


    }

    public void main() {

        Handler handler1 = new Handler();

            handler1.postDelayed(new Runnable() {

                @Override
                public void run() {
                    int count = 0;
                    try {
                        System.out.println("inside run");
                        for (i = 0;i<4 ;i++ ) {

                             Handler handler2 = new Handler();
                            int x = randomgen();

                            if (x == 1) {
                                b1.setBackgroundColor(Color.BLACK);
                                handler2.postDelayed(new Runnable() {

                                    @Override
                                    public void run() {
                                        b1.setBackgroundResource(android.R.drawable.btn_default);
                                    }
                                }, 2000);


                            } else if (x == 2) {
                                b2.setBackgroundColor(Color.BLACK);
                                handler2.postDelayed(new Runnable() {

                                    @Override
                                    public void run() {
                                        b2.setBackgroundResource(android.R.drawable.btn_default);
                                    }
                                }, 2000);


                            } else if (x == 3) {
                                b3.setBackgroundColor(Color.BLACK);
                                handler2.postDelayed(new Runnable() {

                                    @Override
                                    public void run() {
                                        b3.setBackgroundResource(android.R.drawable.btn_default);
                                    }
                                }, 2000);


                            } else if (x == 4) {
                                b4.setBackgroundColor(Color.BLACK);
                                handler2.postDelayed(new Runnable() {

                                    @Override
                                    public void run() {
                                        b4.setBackgroundResource(android.R.drawable.btn_default);
                                    }
                                }, 2000);


                            }


                        }
                    }catch (Exception e) {
                        System.out.println("inside catch");
                        e.printStackTrace();
                    }

                }
            }, 1000 );


    }

1 个答案:

答案 0 :(得分:1)

你这样做了四次:

for (i = 0;i<4 ;i++ ) { ... }

所以难怪,最多4个按钮会改变颜色。它有时候会减少,因为有时你会不止一次得到相同的随机数。