簡體   English   中英

在 Android 中的兩個活動之間切換時更改布局中的可見性 TextView

[英]Change visibility TextView in layout when switch between two activities in Android

我有一個應用程序,它在 Android 中的兩個不同活動(帶有簡單文本的主要活動和帶有兩個 TextViews 的第二個活動)之間切換,每 10 秒具有不同的布局。

我想在第二個活動中第一次只顯示第一個 textView A,然后返回 MainActivity 並再次導致第二個活動但只顯示第二個 textView B。

activity_second.xml:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="255dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/holo_purple"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="A"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:textSize="65sp" />
    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/holo_orange_light"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textViewB"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="B"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:textSize="65sp" />
    </LinearLayout>
</LinearLayout>

以及在下面提供的兩個活動之間切換的 java 代碼。

Second_activity.java:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_activity);

    timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            Intent intent = new Intent(SecondActivity.this, MainActivity.class);
            startActivity(intent);

            finish();

        }
    }, 10000);
    ........
}

我的問題是每次在兩個活動之間切換時如何更改兩個文本視圖的可見性。

要做到這一點您可以將一個變量(如果只有 2 個 TextView,它可以是布爾值)傳遞給第二個活動。 如果為true ,則顯示第 1 個 TextView,如果為false ,則顯示第 2 個 TextView。

要有意傳遞價值,請使用

Boolean value = true; //set this if You want to show 1st or 2nd textbox
Intent i = new Intent(CurrentActivity.this, NewActivity.class);    
i.putExtra("key", value);
startActivity(i);

要檢索值:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    Boolean value = extras.getBoolean("key");
    //The key argument here must match that used in the other activity
}

當您擁有該value時,只需設置 TextViews 的可見性:

if (value)
{
    textView1.setVisibility(TextView.VISIBLE);
    textView2.setVisibility(TextView.GONE);
}
else 
{
    textView1.setVisibility(TextView.GONE);
    textView2.setVisibility(TextView.VISIBLE);
}

暫無
暫無

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

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