Android使用setContentView實(shí)現(xiàn)頁(yè)面的轉(zhuǎn)換效果
一提到Android中頁(yè)面的切換,你是不是只想到了startActivity啟動(dòng)另一個(gè)Activity?
其實(shí)在Android中,可以直接利用setContentView達(dá)到類(lèi)似頁(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)類(lèi)似相互嵌套調(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">
<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、第二個(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: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>
通過(guò)setContentView實(shí)現(xiàn)頁(yè)面切換,相比Activity切換有個(gè)特別的優(yōu)點(diǎn):
所有程序里的變量都存在相同的狀態(tài):類(lèi)成員變量、類(lèi)函數(shù)等,都可以在同一個(gè)Activity中直接獲得,沒(méi)有參數(shù)傳遞的問(wèn)題。比如:
Layout1收集了用戶(hù)輸入的銀行卡號(hào)碼等付款信息,點(diǎn)擊“下一步”進(jìn)入Layout2顯示訂單信息,讓用戶(hù)確認(rèn),用戶(hù)點(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)注腳本之家其它相關(guān)文章!
- 源碼詳解Android中View.post()用法
- Android自定義View仿大眾點(diǎn)評(píng)星星評(píng)分控件
- Android 滑動(dòng)Scrollview標(biāo)題欄漸變效果(仿京東toolbar)
- Android使用ScrollView實(shí)現(xiàn)滾動(dòng)效果
- Android 中 WebView 的基本用法詳解
- Android使用TypeFace設(shè)置TextView的文字字體
- Android自定義view之太極圖的實(shí)現(xiàn)教程
- Android自定義view之圍棋動(dòng)畫(huà)效果的實(shí)現(xiàn)
- Android自定義View實(shí)現(xiàn)分段選擇按鈕的實(shí)現(xiàn)代碼
- Android View.Post 的原理及缺陷
相關(guān)文章
Android 使用Zbar實(shí)現(xiàn)掃一掃功能
這篇文章主要介紹了Android 使用Zbar實(shí)現(xiàn)掃一掃功能,本文用的是Zbar實(shí)現(xiàn)掃一掃,因?yàn)楦鶕?jù)本人對(duì)兩個(gè)庫(kù)的使用比較,發(fā)現(xiàn)Zbar解碼比Zxing速度要快,實(shí)現(xiàn)方式也簡(jiǎn)單,需要的朋友可以參考下2023-03-03
Android獲得當(dāng)前正在顯示的activity類(lèi)名的方法
這篇文章主要介紹了Android獲得當(dāng)前正在顯示的activity類(lèi)名的方法,分析了權(quán)限的修改與Java代碼的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01
Android實(shí)現(xiàn)九宮格手勢(shì)解鎖
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)九宮格手勢(shì)解鎖的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
android教程之使用asynctask在后臺(tái)運(yùn)行耗時(shí)任務(wù)
AsyncTask用在需要在ui線(xiàn)程中調(diào)用、在背景線(xiàn)程中執(zhí)行耗時(shí)任務(wù)、并且在ui線(xiàn)程中返回結(jié)果的場(chǎng)合。下面就是一個(gè)在背景中運(yùn)行的AsyncTask的實(shí)現(xiàn)DownloadDBTask2014-02-02
點(diǎn)擊微信內(nèi)網(wǎng)頁(yè)a標(biāo)簽直接跳轉(zhuǎn)打開(kāi)淘寶APP的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于如何實(shí)現(xiàn)點(diǎn)擊微信內(nèi)網(wǎng)頁(yè)a標(biāo)簽直接跳轉(zhuǎn)打開(kāi)淘寶APP的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-11-11
Android開(kāi)發(fā)進(jìn)階自定義控件之滑動(dòng)開(kāi)關(guān)實(shí)現(xiàn)方法【附demo源碼下載】
這篇文章主要介紹了Android開(kāi)發(fā)進(jìn)階自定義控件之滑動(dòng)開(kāi)關(guān)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Android自定義開(kāi)關(guān)控件的原理、實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-08-08
Android使用自定義View實(shí)現(xiàn)橫行時(shí)間軸效果
這篇文章主要給大家介紹了關(guān)于Android使用自定義View實(shí)現(xiàn)橫行時(shí)間軸效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Android具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

