0

我想做应用程序来显示 3 个元素(每 2 秒),并且每个元素都在之前的元素之后开始显示。这是代码:

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);

}

但所有元素同时可见

4

0 回答 0