繁体   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