如何以编程方式隐藏Imageview

时间:2012-11-29 06:07:26

标签: android

我有一个带有三个图标的线性布局,如下所示

 <ImageView
            android:id="@+id/cities"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content          
            android:src="@drawable/city" />

        <ImageView
            android:id="@+id/red"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"           
            android:src="@drawable/red"
            android:visibility="gone" 
            />
        <ImageView
            android:id="@+id/deal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"       
            android:src="@drawable/deal" />

最初中间的图标是隐藏的(android:visibility =“gone”)我有一个登录屏幕..当登录我成功时我希望图标可见..如下所示..但它不工作

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.fyelogin);                    
    etPassword = (EditText)findViewById(R.id.password);
    btnLogin = (Button)findViewById(R.id.login_button);
    btnCancel = (Button)findViewById(R.id.cancel_button);
    lblResult = (TextView)findViewById(R.id.result);

    final ImageView details = (ImageView)findViewById(R.id.red); 

    btnLogin.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            String password = etPassword.getText().toString();

            if(password.equals("guest")){
                lblResult.setText("password successful.");

    giving error @ this line  ----->  details.setVisibility(View.VISIBLE);

            } else {
                lblResult.setText("password doesn't match.");
            }

            finish();
        }
    });

2 个答案:

答案 0 :(得分:2)

您应该将ImageView详细信息对象设置为类变量,以便以后监听器可以访问它。

答案 1 :(得分:0)

试试这个,

ImageView details;
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fyelogin);                    
        etPassword = (EditText)findViewById(R.id.password);
        btnLogin = (Button)findViewById(R.id.login_button);
        btnCancel = (Button)findViewById(R.id.cancel_button);
        lblResult = (TextView)findViewById(R.id.result);
        details = (ImageView)findViewById(R.id.red); 

        btnLogin.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                String password = etPassword.getText().toString();

                if(password.equals("guest")){
                    lblResult.setText("password successful.");

                     details.setVisibility(View.VISIBLE);

                } else {
                    lblResult.setText("password doesn't match.");

                }

                finish();} });