Android使用setContentView實(shí)現(xiàn)頁(yè)面的轉(zhuǎn)換效果
一提到Android中頁(yè)面的切換,你是不是只想到了startActivity啟動(dòng)另一個(gè)Activity?其實(shí)在Android中,可以直接利用setContentView達(dá)到類似頁(yè)面轉(zhuǎn)換效果的!實(shí)現(xiàn)思路如下:
在第一個(gè)Activity的布局中添加一個(gè)Button,實(shí)現(xiàn)點(diǎn)擊事件 點(diǎn)擊該Button,調(diào)用setContentView,傳入第二個(gè)頁(yè)面的Layout,第二個(gè)頁(yè)面就顯示出來(lái)了 第二個(gè)頁(yè)面的布局中仍然有一個(gè)Button,仍然實(shí)現(xiàn)其點(diǎn)擊事件 點(diǎn)擊該Button,調(diào)用setContentView,傳入第一個(gè)頁(yè)面的Layout,第一個(gè)頁(yè)面就顯示回來(lái)了因此,有點(diǎn)類似相互嵌套調(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)到第二個(gè)頁(yè)面jumpToLayout2(); } }); } private void jumpToLayout2() { // 設(shè)置第二個(gè)頁(yè)面的布局 setContentView(R.layout.layout2); Button button2 = findViewById(R.id.buttonGoToLayout1); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {// 在第二個(gè)頁(yè)面中,點(diǎn)擊Button,跳轉(zhuǎn)到第一個(gè)頁(yè)面jumpToLayout1(); } }); } private void jumpToLayout1() { // 設(shè)置第一個(gè)頁(yè)面d的布局 setContentView(R.layout.main_page_layout); Button button = findViewById(R.id.buttonGoToLayout2); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {// 點(diǎn)擊第一個(gè)頁(yè)面的Button,跳轉(zhuǎn)到第二個(gè)頁(yè)面jumpToLayout2(); } }); }}兩個(gè)布局文件如下:
1、第一個(gè)頁(yè)面布局: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、第二個(gè)頁(yè)面布局: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>
通過(guò)setContentView實(shí)現(xiàn)頁(yè)面切換,相比Activity切換有個(gè)特別的優(yōu)點(diǎn):
所有程序里的變量都存在相同的狀態(tài):類成員變量、類函數(shù)等,都可以在同一個(gè)Activity中直接獲得,沒(méi)有參數(shù)傳遞的問(wèn)題。比如:
Layout1收集了用戶輸入的銀行卡號(hào)碼等付款信息,點(diǎn)擊“下一步”進(jìn)入Layout2顯示訂單信息,讓用戶確認(rèn),用戶點(diǎn)擊“確認(rèn)”按鈕后,進(jìn)入Layout3進(jìn)行付款的授權(quán)操作,整個(gè)過(guò)程沒(méi)有變量的傳遞。
以上就是Android使用setContentView實(shí)現(xiàn)頁(yè)面的轉(zhuǎn)換效果的詳細(xì)內(nèi)容,更多關(guān)于Android 頁(yè)面轉(zhuǎn)換效果的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 使用Python webdriver圖書(shū)館搶座自動(dòng)預(yù)約的正確方法2. Python字符串到字節(jié)的轉(zhuǎn)換。雙反斜杠問(wèn)題3. Python3 json模塊之編碼解碼方法講解4. python 使用事件對(duì)象asyncio.Event來(lái)同步協(xié)程的操作5. Python 合并拼接字符串的方法6. Linux刪除系統(tǒng)自帶版本Python過(guò)程詳解7. Python sublime安裝及配置過(guò)程詳解8. ASP.NET MVC使用jQuery ui的progressbar實(shí)現(xiàn)進(jìn)度條9. Java Long類型對(duì)比分析10. ASP基礎(chǔ)知識(shí)VBScript基本元素講解

網(wǎng)公網(wǎng)安備