简体   繁体   English

将FrameLayout设置为在单击按钮时可见

[英]Set FrameLayout to visible on button click

I'm trying to create an app which allows multiple FrameLayouts to appear one at a time on button click. 我正在尝试创建一个应用程序,该应用程序允许在单击按钮时一次显示多个FrameLayout。

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. 当我单击按钮时,这基本上将使3 FrameLayouts可见,这是显而易见的。 I basically want each FrameLayout to appear one at a time ie one click of the button would generate frameLayout2, another click of the button frameLayout3, and a final click frameLayout4. 我基本上希望每个FrameLayout一次出现一次,即,一次单击按钮将生成frameLayout2,再次单击按钮frameLayout3,最后单击一次frameLayout4。

Thanks for any help! 谢谢你的帮助!

You should... 你应该...

A) Reverse the order of your if statements or B) Use a switch statement for the count instead (Better way) A)反转if语句的顺序,或B)改为使用switch语句进行计数(更好的方式)

You are basically calling all the if statements, because you aren't breaking out of whatever loop you have when count is incremented. 基本上,您正在调用所有的if语句,因为当计数增加时,您不会脱离任何循环。

Change count to a member and eventually reset it if it reaches 3. Then add else's to your if's like this: count更改为成员,如果count达到3,则最终将其重置。然后,将if添加为else,如下所示:

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. 您可能希望在每个if块中将一个FrameLayout设置为View.VISIBLE ,将另外两个设置为View.GONE ,具体取决于您要实现的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM