setContentViewを使用して、ページ変換効果を実現します

Androidでのページ切り替えに関して、startActivityが別のアクティビティを開始することを考えましたか?実際、Androidでは、setContentViewを直接使用して、同様のページ変換効果を実現できます。実現のアイデアは次のとおりです。

  1. クリックイベントを実装するために、最初のアクティビティのレイアウトにボタンを追加します
  2. ボタンをクリックし、setContentViewを呼び出し、2番目のページのレイアウトを渡すと、2番目のページが表示されます。
  3. 2ページ目のレイアウトにはまだボタンがあり、そのクリックイベントは引き続き実装されています
  4. ボタンをクリックし、setContentViewを呼び出し、最初のページのレイアウトを渡すと、最初のページが表示されます。

したがって、ネストされた呼び出しに似ており、ソースコードは次のとおりです。

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

2つのレイアウトファイルは次のとおりです
。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. 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によるページ切り替えには、アクティビティ切り替えと比較して特別な利点があり
ます。プログラム内のすべての変数は同じ状態です。クラスメンバー変数、クラス関数などは、同じアクティビティで直接取得でき、パラメーターの受け渡しの問題はありません。


Layout1は、ユーザーが入力した銀行カード番号などの支払い情報を収集します。[次へ]をクリックしてLayout2に入り、注文情報を表示してユーザーに確認させます。ユーザーが[確認]ボタンをクリックした後、Layout3と入力して支払いを承認します。 。プロセス全体で可変送信はありません。
 

おすすめ

転載: blog.csdn.net/Xia_Leon/article/details/112689586