Domanda

I'm trying to create an app which allows multiple FrameLayouts to appear one at a time on button click.

At the moment the code I have is:

int count = 0;

    if(count == 0 && view.isEnabled()) {
        FrameLayout addActivities = (FrameLayout)findViewById(R.id.frameLayout2);
        addActivities.setVisibility(View.VISIBLE);  
        count++;
    }   
    if (count == 1 && view.isEnabled()) {       
        FrameLayout addActivities2 = (FrameLayout)findViewById(R.id.frameLayout3);
        addActivities2.setVisibility(View.VISIBLE);
        count++;
    }
    if(count == 2 && view.isEnabled()) {
        FrameLayout addActivities3 = (FrameLayout)findViewById(R.id.frameLayout4);
        addActivities3.setVisibility(View.VISIBLE);
    }

This will basically make 3 FrameLayouts visible when I click the button, which is fairly obvious. I basically want each FrameLayout to appear one at a time i.e. one click of the button would generate frameLayout2, another click of the button frameLayout3, and a final click frameLayout4.

Thanks for any help!

È stato utile?

Soluzione

You should...

A) Reverse the order of your if statements or B) Use a switch statement for the count instead (Better way)

You are basically calling all the if statements, because you aren't breaking out of whatever loop you have when count is incremented.

Altri suggerimenti

Change count to a member and eventually reset it if it reaches 3. Then add else's to your if's like this:

if (count == 0 && ...) {
    ...
} else if (count == 1 && ...) {
    ...
} else if (count == 2 && ... ) {
    ...
}

You might want to set one FrameLayout to View.VISIBLE and the other two as View.GONE in each if-block, depending on the effect you want to achieve.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top