午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

Android使用setContentView實現頁面的轉換效果

瀏覽:25日期:2022-09-21 09:27:51

一提到Android中頁面的切換,你是不是只想到了startActivity啟動另一個Activity?其實在Android中,可以直接利用setContentView達到類似頁面轉換效果的!實現思路如下:

在第一個Activity的布局中添加一個Button,實現點擊事件 點擊該Button,調用setContentView,傳入第二個頁面的Layout,第二個頁面就顯示出來了 第二個頁面的布局中仍然有一個Button,仍然實現其點擊事件 點擊該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'> <TextViewandroid: android:layout_width='match_parent'android:layout_height='wrap_content'android:text='This is Layout One'android:paddingTop='20dp'android:textSize='30sp'/> <Buttonandroid:text='Go to Layout Two'android:layout_width='wrap_content'android:layout_height='wrap_content'android: 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: 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: android:layout_marginTop='20dp' android:layout_below='@id/textView2'/></RelativeLayout>

通過setContentView實現頁面切換,相比Activity切換有個特別的優點:

所有程序里的變量都存在相同的狀態:類成員變量、類函數等,都可以在同一個Activity中直接獲得,沒有參數傳遞的問題。比如:

Layout1收集了用戶輸入的銀行卡號碼等付款信息,點擊“下一步”進入Layout2顯示訂單信息,讓用戶確認,用戶點擊“確認”按鈕后,進入Layout3進行付款的授權操作,整個過程沒有變量的傳遞。

以上就是Android使用setContentView實現頁面的轉換效果的詳細內容,更多關于Android 頁面轉換效果的資料請關注好吧啦網其它相關文章!

標簽: Android
相關文章:
主站蜘蛛池模板: 欧美午夜在线 | 黄色在线一区 | 欧美日韩看片 | 天天操夜夜操视频 | 亚洲黄色成人 | 国产精品综合久久久久久 | 综合久久综合久久 | 69性视频 | 精品久久久久久一区二区里番 | 欧美久久久久久久 | 人人狠狠| 无套白嫩进入乌克兰美女 | 久久精品成人一区二区三区蜜臀 | 国产精品视频自拍 | 亚洲精品免费在线 | 精品国产户外野外 | 草久久久久 | 日本裸体网站 | 伊人网在线 | 国产一二三四五区 | 中文字幕乱码在线 | 国语久久| www.婷婷.com | 99精品一区二区 | 91色国产 | 毛片aaaaaa | 69xxx免费视频 | 国产精品日韩欧美一区二区三区 | 99精品国产一区二区 | 国产特黄毛片 | 国产精品国产精品国产专区蜜臀ah | 九九九九精品 | 亚洲黄色在线播放 | 亚洲涩涩网 | 黄色一级大片在线免费看国产一 | 好了av在线| 成年人在线免费 | 欧美视频网站 | 不卡视频一区二区 | 成人在线观看网址 | 亚洲视频在线观看免费 |