0

这是我的代码:

arrow.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub

        String user = user_name.getText().toString();
        String pass = pass_word.getText().toString();

        String check = db.getPassword(user);

        if(check.equals(pass))
        {
            user_name.setVisibility(View.INVISIBLE);
            pass_word.setVisibility(View.INVISIBLE);
            arrow.setVisibility(View.INVISIBLE);

            pg.setVisibility(View.VISIBLE);

            for(int i=0;i<100000;i++)
            {
                pg.setVisibility(View.VISIBLE);
            }

            Intent openQr_code = new Intent ("com.cinamon.vampskin.MENU_ADMIN");
            startActivity(openQr_code);

        }
        else
        {
            user_name.setVisibility(View.INVISIBLE);
            pass_word.setVisibility(View.INVISIBLE);
            arrow.setVisibility(View.INVISIBLE);

            pg.setVisibility(View.VISIBLE);

            for(int j=0;j<100000;j++)
            {
                pg.setVisibility(View.VISIBLE);
            }

            Toast.makeText(getApplicationContext(), "Invalid username or password", Toast.LENGTH_SHORT).show();
            user_name.setVisibility(View.VISIBLE);
            pass_word.setVisibility(View.VISIBLE);
            arrow.setVisibility(View.VISIBLE);

            pg.setVisibility(View.INVISIBLE);

            noOfTimesLoginError++;

            if(noOfTimesLoginError == 3)
            {
                Toast.makeText(getApplicationContext(), "Try again in 10 seconds...", Toast.LENGTH_LONG).show();
                user_name.setVisibility(View.INVISIBLE);
                pass_word.setVisibility(View.INVISIBLE);
                arrow.setVisibility(View.INVISIBLE);

                pg.setVisibility(View.VISIBLE);
                time_text.setVisibility(View.VISIBLE);

                for(int time = 10 ; time>0 ; time--)
                {
                    for(int m = 0 ; m < 1000 ; m++)
                    {
                        time_text.setText("00 : 00 : "+time);
                    }
                }

                user_name.setVisibility(View.VISIBLE);
                pass_word.setVisibility(View.VISIBLE);
                arrow.setVisibility(View.VISIBLE);
                pg.setVisibility(View.INVISIBLE);
                time_text.setVisibility(View.INVISIBLE);
            }
        }
        return false;
    }
});

在其他部分,我想隐藏用户名字段、密码字段、图像箭头和 pg 是进度条,这些必须隐藏。在 for 循环之后只应该显示它,但是当我运行这段代码时,不可见的不工作,那些总是可见的。但在第一个 if 部分,根据代码可以完美地工作。

请帮忙,提前谢谢你:)

4

2 回答 2

0

原因是您希望视图立即变得不可见,但这不起作用,通常它们在下一个渲染周期中变得不可见,但是您不给系统重绘视图的机会,因为您的主线程是忙于

            for(int time = 10 ; time>0 ; time--)
            {
                for(int m = 0 ; m < 1000 ; m++)
                {
                    time_text.setText("00 : 00 : "+time);
                }
            }

然后您将可见性切换回

            user_name.setVisibility(View.VISIBLE);
            pass_word.setVisibility(View.VISIBLE);
            arrow.setVisibility(View.VISIBLE);

所以它仍然可见

于 2013-07-15T13:07:13.227 回答
0

有同样的问题,正如@Desert 所说,它应该在不同的渲染周期中完成。为此,您可以添加一个带有 postDelay 的处理程序,这对我有用。

new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // Turn view visible/invisible
                    }
                }, 500); // Change the time for as little as long it works
于 2019-05-01T10:44:50.530 回答