简体   繁体   English

如何在Button上隐藏或显示元素,单击Next Activity

[英]How to hide or show element on Button click on Next Activity

I was curious. 我很好奇 Consider I have One SecondActivity with ProgressBar which layout file is 考虑我有一个带有ProgressBar SecondActivity ,哪个布局文件是

<ProgressBar
    android:id="@+id/progressbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />

and In MainActivity I have Two Buttons MainActivity我有两个按钮

<Button
    android:id="@+id/Button 1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/Button 2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Both Button when clicked will open SecondActivity . 单击两个按钮都将打开SecondActivity

Now I would Like to show progress bar when button A is clicked but when Button B is clicked, Make the progress Bar invisible in Second Activity. 现在,我想在单击按钮A时显示进度条,但是在单击按钮B时,使“进度条”在“第二个活动”中不可见。

Below is diagrams illustrating above method that I would like to get. 下图说明了我想获得的上述方法。

Screenshot: 截图:

You just have to pass some extras along with the intent while launching your second activity. 在启动第二个活动时,您只需要传递一些额外的意图。

When button A is clicked, start the second activity like this. 当单击按钮A时,像这样开始第二个活动。

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("SHOW_PROGRESS", true);
startActivity(intent);

And when the button B is clicked, launch the second activity without passing any extra in your intent like the following. 然后,当单击按钮B时,启动第二个活动,而不会像下面那样传递您的意图。

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);

Now from your SecondActivity receive the intent and show the ProgressBar based on the value found from your extras. 现在,从您的SecondActivity接收意图,并根据从其他项目中找到的值显示ProgressBar

boolean showProgressBar = getIntent().getBooleanExtra("SHOW_PROGRESS", false);

if(showProgressBar) progressBar.show();

Hope that helps! 希望有帮助!

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

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