Android使用setContentView實現(xiàn)頁面的轉(zhuǎn)換效果
一提到Android中頁面的切換,你是不是只想到了startActivity啟動另一個Activity?
其實在Android中,可以直接利用setContentView達(dá)到類似頁面轉(zhuǎn)換效果的!實現(xiàn)思路如下:
- 在第一個Activity的布局中添加一個Button,實現(xiàn)點擊事件
- 點擊該Button,調(diào)用setContentView,傳入第二個頁面的Layout,第二個頁面就顯示出來了
- 第二個頁面的布局中仍然有一個Button,仍然實現(xiàn)其點擊事件
- 點擊該Button,調(diào)用setContentView,傳入第一個頁面的Layout,第一個頁面就顯示回來了
因此,有點類似相互嵌套調(diào)用,源代碼如下:
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) { // 跳轉(zhuǎn)到第二個頁面 jumpToLayout2(); } }); } private void jumpToLayout2() { // 設(shè)置第二個頁面的布局 setContentView(R.layout.layout2); Button button2 = findViewById(R.id.buttonGoToLayout1); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 在第二個頁面中,點擊Button,跳轉(zhuǎn)到第一個頁面 jumpToLayout1(); } }); } private void jumpToLayout1() { // 設(shè)置第一個頁面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,跳轉(zhuǎn)到第二個頁面 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實現(xiàn)頁面切換,相比Activity切換有個特別的優(yōu)點:
所有程序里的變量都存在相同的狀態(tài):類成員變量、類函數(shù)等,都可以在同一個Activity中直接獲得,沒有參數(shù)傳遞的問題。比如:
Layout1收集了用戶輸入的銀行卡號碼等付款信息,點擊“下一步”進(jìn)入Layout2顯示訂單信息,讓用戶確認(rèn),用戶點擊“確認(rèn)”按鈕后,進(jìn)入Layout3進(jìn)行付款的授權(quán)操作,整個過程沒有變量的傳遞。
以上就是Android使用setContentView實現(xiàn)頁面的轉(zhuǎn)換效果的詳細(xì)內(nèi)容,更多關(guān)于Android 頁面轉(zhuǎn)換效果的資料請關(guān)注腳本之家其它相關(guān)文章!
- 源碼詳解Android中View.post()用法
- Android自定義View仿大眾點評星星評分控件
- Android 滑動Scrollview標(biāo)題欄漸變效果(仿京東toolbar)
- Android使用ScrollView實現(xiàn)滾動效果
- Android 中 WebView 的基本用法詳解
- Android使用TypeFace設(shè)置TextView的文字字體
- Android自定義view之太極圖的實現(xiàn)教程
- Android自定義view之圍棋動畫效果的實現(xiàn)
- Android自定義View實現(xiàn)分段選擇按鈕的實現(xiàn)代碼
- Android View.Post 的原理及缺陷
相關(guān)文章
詳解Android中Intent傳遞對象給Activity的方法
這篇文章主要介紹了Android中Intent傳遞對象給Activity的方法,文章中對Activity的生命周期等知識先作了簡要的介紹,需要的朋友可以參考下2016-04-04AndroidStudio集成OpenCV的實現(xiàn)教程
本文主要介紹了Android?Studio集成OpenCV的實現(xiàn)教程,文中通過圖文介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12Android 獲取屏幕高度,標(biāo)題高度,狀態(tài)欄高度(實例代碼)
getWindow().findViewById(Window.ID_ANDROID_CONTENT)這個方法獲取到的view就是程序不包括標(biāo)題欄的部分,然后就可以知道標(biāo)題欄的高度了2013-11-11Android中使用imageviewswitcher 實現(xiàn)圖片切換輪播導(dǎo)航的方法
ImageSwitcher是Android中控制圖片展示效果的一個控件。本文給大家介紹Android中使用imageviewswitcher 實現(xiàn)圖片切換輪播導(dǎo)航的方法,需要的朋友參考下吧2016-12-12Android中使用 AutoCompleteTextView 實現(xiàn)手機(jī)號格式化附帶清空歷史的操作
有個小伙伴遇到了這樣一個問題,就是AutoCompleteTextView實現(xiàn)自動填充的功能。同時要具備手機(jī)格式化的功能。接下來通過本文給大家分享使用 AutoCompleteTextView 實現(xiàn)手機(jī)號格式化附帶清空歷史的操作方法,需要的朋友參考下2017-03-03Android IPC機(jī)制利用Messenger實現(xiàn)跨進(jìn)程通信
這篇文章主要介紹了Android IPC機(jī)制中 Messager 實現(xiàn)跨進(jìn)程通信的知識,對Android學(xué)習(xí)通信知識非常重要,需要的同學(xué)可以參考下2016-07-07