簡體   English   中英

只能在第一次點擊時導航到額外的布局

[英]Only able to navigate to the extra layout for the first click

以下代碼是微不足道的,但模擬了問題。

活動_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnGoToExtra"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="266dp"
        android:text="Go To Extra"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

layout_extra.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btnBackToMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="186dp"
        android:text="Back To Main"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

主活動.java

public class MainActivity extends AppCompatActivity {

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

        findViewById(R.id.btnGoToExtra)
                .setOnClickListener(v -> {
                    setContentView(R.layout.layout_extra);
                    findViewById(R.id.btnBackToMain)
                            .setOnClickListener(vv -> {
                                setContentView(R.layout.activity_main);
                            });
                });
    }
}

問題

我可以從activity_main導航到layout_extra並返回到activity_main 不幸的是,在這次往返之后,我無法再導航到layout_extra

是什么導致了這個問題以及如何解決它?

解釋將在聊天中進行,但這不是應該如何完成的,對於要更改的視圖,您還有其他選擇,例如使用 Fragment 或完全使用新活動,我還建議您轉向 kotlin。

public class MainActivity extends AppCompatActivity {
    View mainView;
    View extraView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainView = getLayoutInflater().inflate(R.layout.activity_main, null);
        extraView = getLayoutInflater().inflate(R.layout.layout_extra, null);
        setContentView(mainView);

        mainView.findViewById(R.id.btnGoToExtra)
                .setOnClickListener(v -> {
                    setContentView(extraView);
                });
        extraView.findViewById(R.id.btnBackToMain)
                .setOnClickListener(vv -> {
                    setContentView(mainView);
                });
    }
}

通讀上面的片段,這里是一切的進展

findViewById(R.id.btnGoToExtra)
               .setOnClickListener

使用 id btnGoToExtra為單擊事件注冊按鈕,當發生這種情況時,您有嵌套的

findViewById(R.id.btnBackToMain)
                            .setOnClickListener

它使用 id backToMain為點擊事件注冊元素,僅此而已

不建議多次調用setContentView() 在同一個 Activity 中切換布局的正常方法是使用 ViewFlipper 或 FrameLayout

這里

然后你有:

XML

<FrameLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView 
        android:src="@drawable/icon"
        android:scaleType="fitCenter"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"/>
    <TextView
        android:text="Learn-Android.com"
        android:textSize="24sp"
        android:textColor="#000000"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:gravity="center"/>
</FrameLayout>

代碼

private void SwitchLayout2() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);

// Enable Layout 2 and Disable Layout 1
Layout1 .setVisibility(View.GONE);
Layout2.setVisibility(View.VISIBLE);
}

private void SwitchLayout1() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);

// Enable Layout 1 & Disable Layout2
Layout1.setVisibility(View.VISIBLE);
Layout2.setVisibility(View.GONE);
}

暫無
暫無

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

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