Android – how do I display buttons under conditions?

I have a button that basically looks like this:

@H_502_5@ <Button
    android:id="@+id/admin_new_questions"        
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="See Asked Questions"
    />    

I try to display it only in some cases:

@H_502_5@if ( clause )
{
        Button admin_see_questions = (Button)findViewById(R.id.admin_new_questions);   
        admin_see_questions.setOnClickListener(new Button.OnClickListener() 
        {  
            public void onClick(View v) 
            {
           ....    
            }
        });        
}

However, for some reason, the button will be displayed in all cases, but if the clause is an error, the listener will not be listened

If this clause is true, how can I make the button display?

thank you!

resolvent:

Your button is in an XML layout, so you can hide or show it by changing its visibility

Note: you only need to do the following:

>Use findviewbyid() to get a reference to the button > set onclicklistener for the button

@H_502_5@<Button
android:id="@+id/admin_new_questions"        
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="See Asked Questions"

android:visibility="invisible" //Initially hide the button

/>    

@H_502_5@Button admin_see_questions = (Button)findViewById(R.id.admin_new_questions);   
admin_see_questions.setOnClickListener(new Button.OnClickListener() 
{  
    public void onClick(View v) 
    {
        ....       
    }
});  

if ( clause )
{
    admin_see_questions.setVisibility(View.VISIBLE); //SHOW the button
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>