简体   繁体   中英

Is there any simple and accurate way to do this. I'm trying to develop a some kind of vip light android app

Is there a way to do this with a single handler.post() or any accurate way than my code. I'm trying to develop a some kind of vip light

Here is my onCreate method:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    t1 = (TextView) findViewById(R.id.txt1);
    t2 = (TextView) findViewById(R.id.txt2);
    handler = new Handler();
    Thread t=new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            t1.setBackgroundColor(Color.RED);

                        }
                    });

                    Thread.sleep(100);

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            t1.setBackgroundColor(Color.BLACK);

                        }
                    });

                    Thread.sleep(100);
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            t1.setBackgroundColor(Color.RED);

                        }
                    });

                    Thread.sleep(100);

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            t1.setBackgroundColor(Color.BLACK);

                        }
                    });

                    Thread.sleep(100);

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            t2.setBackgroundColor(Color.BLUE);

                        }
                    });

                    Thread.sleep(100);

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            t2.setBackgroundColor(Color.BLACK);

                        }
                    });

                    Thread.sleep(100);

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            t2.setBackgroundColor(Color.BLUE);

                        }
                    });

                    Thread.sleep(100);

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            t2.setBackgroundColor(Color.BLACK);

                        }
                    });

                    Thread.sleep(100);


                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    t.start();
}

You can use a function:

public void changeColor(final TextView textView, final int color) {
        textView.post(new Runnable() {
            @Override
            public void run() {
                textView.setBackgroundColor(color);
            }
        });
    }

Example:

int[] colors = new int[] {Color.RED, Color.BLACK, Color.RED, Color.BLACK, Color.BLUE, Color.BLACK, Color.BLUE, Color.BLACK};

    final TextView t1 = (TextView) findViewById(R.id.txt1);
    final TextView t2 = (TextView) findViewById(R.id.txt2);

    Thread t=new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    for (int i = 0; i < colors.length; i++) {
                        changeColor(i % 2  == 0 ? t1 : t2, colors[i]);

                        Thread.sleep(100);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    t.start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM