Android使用setContentView實現(xiàn)頁面的轉換效果
一提到Android中頁面的切換,你是不是只想到了startActivity啟動另一個Activity?
其實在Android中,可以直接利用setContentView達到類似頁面轉換效果的!實現(xiàn)思路如下:
- 在第一個Activity的布局中添加一個Button,實現(xiàn)點擊事件
- 點擊該Button,調用setContentView,傳入第二個頁面的Layout,第二個頁面就顯示出來了
- 第二個頁面的布局中仍然有一個Button,仍然實現(xiàn)其點擊事件
- 點擊該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">
<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收集了用戶輸入的銀行卡號碼等付款信息,點擊“下一步”進入Layout2顯示訂單信息,讓用戶確認,用戶點擊“確認”按鈕后,進入Layout3進行付款的授權操作,整個過程沒有變量的傳遞。
以上就是Android使用setContentView實現(xiàn)頁面的轉換效果的詳細內容,更多關于Android 頁面轉換效果的資料請關注腳本之家其它相關文章!
相關文章
android教程之使用asynctask在后臺運行耗時任務
AsyncTask用在需要在ui線程中調用、在背景線程中執(zhí)行耗時任務、并且在ui線程中返回結果的場合。下面就是一個在背景中運行的AsyncTask的實現(xiàn)DownloadDBTask2014-02-02
Android開發(fā)進階自定義控件之滑動開關實現(xiàn)方法【附demo源碼下載】
這篇文章主要介紹了Android開發(fā)進階自定義控件之滑動開關實現(xiàn)方法,結合實例形式詳細分析了Android自定義開關控件的原理、實現(xiàn)步驟與相關操作技巧,需要的朋友可以參考下2016-08-08
Android使用自定義View實現(xiàn)橫行時間軸效果
這篇文章主要給大家介紹了關于Android使用自定義View實現(xiàn)橫行時間軸效果的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Android具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-12-12

