我很好奇。想想我有一个SecondActivity
与ProgressBar
它的布局文件
<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" />
而在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" />
单击两个 Button 时都会打开SecondActivity
。
现在我想在单击按钮 A 时显示进度条,但是当单击按钮 B 时,使进度条在第二个活动中不可见。
下面是说明我想得到的上述方法的图表。
截屏:
你只需要在启动第二个活动时传递一些额外的内容和意图。
当单击按钮 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.
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.
boolean showProgressBar = getIntent().getBooleanExtra("SHOW_PROGRESS", false);
if(showProgressBar) progressBar.show();
Hope that helps!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句