始终可见textview,隐形不在android中工作

时间:2013-07-15 12:49:16

标签: android

这是我的代码:

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循环之后,只应显示它,但是当我运行此代码时,隐形不起作用,这些代码始终可见。但首先,如果是部分,这完全符合代码。

请帮助,提前谢谢:)

2 个答案:

答案 0 :(得分: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);

所以它保留了可见

答案 1 :(得分: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