在Android中切换两个活动时更改布局中TextView的可见性。

huangapple go评论75阅读模式
英文:

Change visibility TextView in layout when switch between two activities in Android

问题

您想要在第二个活动中,每次切换到该活动时只显示一个TextView A或B。您可以通过以下方式实现:

Second_activity.java中的onCreate方法中,添加一个变量来跟踪当前应该显示的TextView,然后根据需要设置其可见性。以下是示例代码:

private TextView currentTextView; // 添加一个成员变量来跟踪当前的TextView

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

    // 初始化currentTextView为第一个TextView(A)
    currentTextView = findViewById(R.id.textView);

    timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // 在此处切换到MainActivity前,设置当前TextView不可见
                    currentTextView.setVisibility(View.INVISIBLE);

                    // 根据当前TextView切换到另一个TextView
                    if (currentTextView == findViewById(R.id.textView)) {
                        currentTextView = findViewById(R.id.textViewB); // 切换到第二个TextView(B)
                    } else {
                        currentTextView = findViewById(R.id.textView); // 切换回第一个TextView(A)
                    }

                    // 设置新的当前TextView可见
                    currentTextView.setVisibility(View.VISIBLE);
                }
            });

            Intent intent = new Intent(SecondActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }, 10000);
    // ........
}

这段代码会在每次切换到MainActivity之前根据当前的TextView设置可见性,然后切换到另一个TextView,以便在下次切换到Second Activity时显示不同的TextView。

英文:

I have an app that switches between two different activities (Main Activity with a simple text and Second Activity with two TextViews) in Android with different layouts every 10 seconds.

I want in Second Activity at the first time display only the first textView A, then return to the MainActivity and again lead to the Second Activity but display only the second 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>

And java code that switches between two Activities provided bellow.

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);
    ........
}

My problem is how to change the visibility of the two textViews for every time that switches between two activities.

答案1

得分: 1

To do it You can just pass a variable (if there are only 2 TextViews it can be boolean) to second activity. If it is true show 1st TextView if it is false show 2nd TextView.

To pass value with intent use this:

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);

To retrieve the value:

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

When You have that value just set visibility of TextViews:

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

To do it You can just pass a variable (if there are only 2 TextViews it can be boolean) to second activity. If it is true show 1st TextView if it is false show 2nd TextView.

To pass value with intent use this:

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);

To retrieve the value:

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

When You have that value just set visibility of TextViews:

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

huangapple
  • 本文由 发表于 2020年7月23日 03:04:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63041452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定