android - 处理程序不在循环中工作

时间:2014-06-02 18:16:18

标签: android android-os-handler

我想做app来显示3个元素(每2秒),每个元素都显示在before元素之后。这是代码:

public void gameStart()
{
    do
    {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable()
        {
            public void run()
            {

                if (data == 1)
                {
                    CountDownTimer cdt = new CountDownTimer(2000, 1000)
                    {

                        @Override
                        public void onTick(long millisUntilFinished)
                        {
                            element1.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onFinish()
                        {
                            element1.setVisibility(View.GONE);

                        }
                    }.start();

                } else if (data == 2)
                {
                    CountDownTimer cdt = new CountDownTimer(2000, 1000)
                    {

                        @Override
                        public void onTick(long millisUntilFinished)
                        {
                            element2.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onFinish()
                        {
                            element2.setVisibility(View.GONE);

                        }
                    }.start();

                } else if (data == 3)
                {
                    CountDownTimer cdt = new CountDownTimer(2000, 1000)
                    {

                        @Override
                        public void onTick(long millisUntilFinished)
                        {
                            element3.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onFinish()
                        {
                            element3.setVisibility(View.GONE);

                        }
                    }.start();

                } else if (data == 4)
                {
                    CountDownTimer cdt = new CountDownTimer(2000, 1000)
                    {

                        @Override
                        public void onTick(long millisUntilFinished)
                        {
                            element4.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onFinish()
                        {
                            element4.setVisibility(View.GONE);

                        }
                    }.start();

                } else if (data == 5)
                {
                    CountDownTimer cdt = new CountDownTimer(2000, 1000)
                    {

                        @Override
                        public void onTick(long millisUntilFinished)
                        {
                            element5.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onFinish()
                        {
                            element5.setVisibility(View.GONE);

                        }
                    }.start();

                } else if (data == 6)
                {
                    CountDownTimer cdt = new CountDownTimer(2000, 1000)
                    {

                        @Override
                        public void onTick(long millisUntilFinished)
                        {
                            element6.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onFinish()
                        {
                            element6.setVisibility(View.GONE);

                        }
                    }.start();

                }

                id = id + 1;
                text.setText("cos " + id);
                data = random.nextInt(6) + 1;
            }
        }, 2000);
    } while (id <= 3);

}

但如果我开始申请,它会开始滞后。应用程序大喊大叫。我该怎么办? 我也试试这个

public void gameStart()
{
    do
    {
                if (data == 1)
                {
                    element1.setVisibility(View.VISIBLE);
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable()
                    {
                        public void run()
                        {
                            element1.setVisibility(View.GONE);
                        }
                    }, 2000);

                } else if (data == 2)
                {
                    element2.setVisibility(View.VISIBLE);
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable()
                    {
                        public void run()
                        {
                            element2.setVisibility(View.GONE);
                        }
                    }, 2000);

                } else if (data == 3)
                {
                    element3.setVisibility(View.VISIBLE);
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable()
                    {
                        public void run()
                        {
                            element3.setVisibility(View.GONE);
                        }
                    }, 2000);

                } else if (data == 4)
                {
                    element4.setVisibility(View.VISIBLE);
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable()
                    {
                        public void run()
                        {
                            element4.setVisibility(View.GONE);
                        }
                    }, 2000);

                } else if (data == 5)
                {
                    element5.setVisibility(View.VISIBLE);
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable()
                    {
                        public void run()
                        {
                            element5.setVisibility(View.GONE);
                        }
                    }, 2000);

                } else if (data == 6)
                {
                    element6.setVisibility(View.VISIBLE);
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable()
                    {
                        public void run()
                        {
                            element6.setVisibility(View.GONE);
                        }
                    }, 2000);
                }

                id = id + 1;
                text.setText("cos " + id);
                data = random.nextInt(6) + 1;


    } while (id <= 3);

}

但所有元素在同一时间都可见

0 个答案:

没有答案