簡體   English   中英

如何在Button上隱藏或顯示元素,單擊Next Activity

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

我很好奇 考慮我有一個帶有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" />

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" />

單擊兩個按鈕都將打開SecondActivity

現在,我想在單擊按鈕A時顯示進度條,但是在單擊按鈕B時,使“進度條”在“第二個活動”中不可見。

下圖說明了我想獲得的上述方法。

截圖:

在啟動第二個活動時,您只需要傳遞一些額外的意圖。

當單擊按鈕A時,像這樣開始第二個活動。

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

然后,當單擊按鈕B時,啟動第二個活動,而不會像下面那樣傳遞您的意圖。

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

現在,從您的SecondActivity接收意圖,並根據從其他項目中找到的值顯示ProgressBar

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

if(showProgressBar) progressBar.show();

希望有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM