Question

I have one Linearlayout - totalincome and another TableLayout normalincometable, which should appear just below the totalincome. normalincometable will be invisible when the program runs. When the user clicks on "totalincome" the table should display. If the user clicks on "totalincome again", the table should disappear. I have tried this code, but It didnt work.

totalincome.setOnClickListener(new OnClickListener() 
{
    @Override
    public void onClick(View v) 
    {
        int x =0;
        // TODO Auto-generated method stub
        if (x==0)
        {
            normalincometable.setVisibility(View.VISIBLE);
            x=1;
        }
        else 
        {
            normalincometable.setVisibility(View.GONE);
            x=0;
        }
    });
}

From this code, I can make the table visible in first click but It doesnt disappear in next click. Are there any options ?

Était-ce utile?

La solution 2

You have declared int x =0; inside onClick method. So, when ever onClick is called, it assigns 0 to "x". Declare it outside at class scope.

Autres conseils

Try this:

@Override
public void onClick(View v) {
  if(normalincometable.getVisibility() == View.VISIBLE) {
    normalincometable.setVisibility(View.GONE);
  } else {
    normalincometable.setVisibility(View.VISIBLE);
  }
}

because you have define x in the button click code so whenever button click it set to 0. define x outside the button click scope.

try this way:put x variable outside the button onclick() or defined x globally

int x =0;
totalincome.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        // TODO Auto-generated method stub
        if (x==0)
        {
        normalincometable.setVisibility(View.VISIBLE);

        x=1;
        }
        else 
        {
            normalincometable.setVisibility(View.GONE);
                                x=0;
                        }


});

}

Use like this:

   int x =0;    
    totalincome.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        // TODO Auto-generated method stub
                        if (x==0)
                        {
                        normalincometable.setVisibility(View.VISIBLE);

                        x=1;
                        }
                        else 
                        {
                            normalincometable.setVisibility(View.GONE);
                                                x=0;
                                        }


                });

            }

Try this

Boolean isFirstTimeClicked=true;
totalincome.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (isFirstTimeClicked)
            {
            normalincometable.setVisibility(View.VISIBLE);
            }
            else 
            {
                normalincometable.setVisibility(View.GONE);

                            }
isFirstTimeClicked=!isFirstTimeClicked;

    });

}

and in your code you have declared int x =0; inside onClick method. So, when ever onClick is called, it assigns 0 to "x". Declare it outside at class scope.

Easiest Method is

button.setVisibility(View.VISIBLE == button.getVisibility() ? View.GONE:View.VISIBLE);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top