Use setContentView to achieve page conversion effect

When it comes to page switching in Android, did you just think of startActivity to start another Activity? In fact, in Android, you can directly use setContentView to achieve a similar page conversion effect! The realization idea is as follows:

  1. Add a Button to the layout of the first Activity to implement the click event
  2. Click the Button, call setContentView, pass in the Layout of the second page, and the second page will be displayed
  3. There is still a Button in the layout of the second page, and its click event is still implemented
  4. Click the Button, call setContentView, pass in the Layout of the first page, and the first page will be displayed back

Therefore, it is similar to nested calls, the source code is as follows:

public class ExampleActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_page_layout);
        
        Button button = findViewById(R.id.buttonGoToLayout2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到第二个页面
                jumpToLayout2();
            }
        });
    }

    private void jumpToLayout2() {
        // 设置第二个页面的布局
        setContentView(R.layout.layout2);
        Button button2 = findViewById(R.id.buttonGoToLayout1);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 在第二个页面中,点击Button,跳转到第一个页面
                jumpToLayout1();
            }
        });
    }

    private void jumpToLayout1() {
        // 设置第一个页面d的布局
        setContentView(R.layout.main_page_layout);
        Button button = findViewById(R.id.buttonGoToLayout2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击第一个页面的Button,跳转到第二个页面
                jumpToLayout2();
            }
        });
    }
}

The two layout files are as follows:
1. The first page layout: main_page_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center">
        <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="This is Layout One"
                android:paddingTop="20dp"
                android:textSize="30sp"/>
        <Button
                android:text="Go to Layout Two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/buttonGoToLayout2"
                android:layout_marginTop="20dp"
                android:layout_below="@id/textView1"/>
</RelativeLayout>

2. The second page layout: layout2.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@android:color/black" >
    <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="This is Layout Two"
            android:paddingTop="20dp"
            android:textColor="@android:color/white"
            android:textSize="30sp"/>
    <Button
            android:text="Go to Layout One"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/buttonGoToLayout1"
            android:layout_marginTop="20dp"
            android:layout_below="@id/textView2"/>
</RelativeLayout>

The renderings are as follows:

 

Page switching through setContentView has a special advantage compared to Activity switching:
all variables in the program have the same state: class member variables, class functions, etc., can be directly obtained in the same Activity, there is no problem of parameter passing .


Layout1 collects the payment information such as the bank card number entered by the user. Click "Next" to enter Layout2 to display the order information and let the user confirm. After the user clicks the "Confirm" button, enter Layout3 to authorize the payment. There is no variable transmission in the whole process .
 

Guess you like

Origin blog.csdn.net/Xia_Leon/article/details/112689586