使用setContentView实现页面的转换效果

一提到Android中页面的切换,你是不是只想到了startActivity启动另一个Activity?其实在Android中,可以直接利用setContentView达到类似页面转换效果的!实现思路如下:

  1. 在第一个Activity的布局中添加一个Button,实现点击事件
  2. 点击该Button,调用setContentView,传入第二个页面的Layout,第二个页面就显示出来了
  3. 第二个页面的布局中仍然有一个Button,仍然实现其点击事件
  4. 点击该Button,调用setContentView,传入第一个页面的Layout,第一个页面就显示回来了

因此,有点类似相互嵌套调用,源代码如下:

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

两个布局文件如下:
1、第一个页面布局: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、第二个页面布局: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>

效果图如下:

通过setContentView实现页面切换,相比Activity切换有个特别的优点:
所有程序里的变量都存在相同的状态:类成员变量、类函数等,都可以在同一个Activity中直接获得,没有参数传递的问题。


Layout1收集了用户输入的银行卡号码等付款信息,点击“下一步”进入Layout2显示订单信息,让用户确认,用户点击“确认”按钮后,进入Layout3进行付款的授权操作,整个过程没有变量的传递。
 

猜你喜欢

转载自blog.csdn.net/Xia_Leon/article/details/112689586
今日推荐