Android實現(xiàn)頁面跳轉
本文實例為大家分享了Android實現(xiàn)頁面跳轉的具體代碼,供大家參考,具體內容如下
一. Android實現(xiàn)頁面跳轉有兩種方式,一種為.MainActivity跳轉;第二種是Relatelayout布局跳轉,首先看第一種方式
1. MainActivity區(qū)域設置
public class MainActivity extends AppCompatActivity {
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? //獲取按鈕
? ? ? ? Button button = findViewById(R.id.button);
? ? ? ? //按鈕進行監(jiān)聽
? ? ? ? button.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? //監(jiān)聽按鈕,如果點擊,就跳轉
? ? ? ? ? ? ? ? Intent intent = new Intent();
? ? ? ? ? ? ? ? //前一個(MainActivity.this)是目前頁面,后面一個是要跳轉的下一個頁面
? ? ? ? ? ? ? ? intent.setClass(MainActivity.this,NextActivity.class);
? ? ? ? ? ? ? ? startActivity(intent);
? ? ? ? ? ? }
? ? ? ? });
? ? }
}2. 這是下一個頁面 的設置
public class NextActivity extends AppCompatActivity {
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? //這個是獲取布局文件的,這里是你下一個頁面的布局文件
? ? ? ? setContentView(R.layout.activity_next);
? ? }
}3. 這是第一個頁面的布局文件
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? tools:context=".MainActivity"> ? ? <RelativeLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent"> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/one" ? ? ? ? ? ? android:layout_width="200dp" ? ? ? ? ? ? android:layout_height="100dp" ? ? ? ? ? ? android:text="這是第一個頁面!" ? ? ? ? ? ? android:textSize="25dp" ? ? ? ? ? ? android:layout_centerInParent="true" ? ? ? ? ? ? /> ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/button" ? ? ? ? ? ? android:layout_width="100dp" ? ? ? ? ? ? android:layout_height="50dp" ? ? ? ? ? ? tools:ignore="MissingConstraints" ? ? ? ? ? ? android:text="跳轉" ? ? ? ? ? ? android:layout_centerHorizontal="true" ? ? ? ? ? ? android:layout_below="@+id/one" ? ? ? ? ? ? /> ? ? </RelativeLayout> </androidx.constraintlayout.widget.ConstraintLayout>
4. 這是第二個頁面的布局文件
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? tools:context=".MainActivity"> ? ? <TextView ? ? ? ? android:id="@+id/two" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="這是第二個頁面!" ? ? ? ? android:textSize="25dp" ? ? ? ? android:textColor="#663399" ? ? ? ? app:layout_constraintBottom_toBottomOf="parent" ? ? ? ? app:layout_constraintLeft_toLeftOf="parent" ? ? ? ? app:layout_constraintRight_toRightOf="parent" ? ? ? ? app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
5. AndroidManifest.xml配置加上第二個頁面的入口

6. 效果圖


二. 第二種方式是通過控制Java布局文件進行布局組合
1. 首先MainActivity文件
public class MainActivity extends AppCompatActivity {
? ? /**
? ? ?* 聲明布局文件
? ? ?* */
? ? RelativeLayout layoutTitle,layoutBox,layoutButton;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? //獲取布局文件
? ? ? ? getwige();
? ? }
? ? /**
? ? ?* 獲取總體布局
? ? ?* */
? ? private void getwige() {
? ? ? ? //獲取標題布局
? ? ? ? getTitles();
? ? ? ? //獲取中間布局
? ? ? ? getBoxs();
? ? ? ? //獲取底部布局
? ? ? ? getButtons();
? ? }
? ? /**
? ? ?* 獲取標題布局
? ? ?* */
? ? public void getTitles(){
? ? ? ? //獲取總布局中的標題布局
? ? ? ? layoutTitle = this.findViewById(R.id.title);
? ? ? ? //初始化一個標題布局類
? ? ? ? Titles title = new Titles(this);
? ? ? ? //進行組合布局
? ? ? ? layoutTitle.addView(title);
? ? }
? ? /**
? ? ?* 獲取標題布局
? ? ?* */
? ? public void getBoxs(){
? ? ? ? //獲取總布局中的中間布局
? ? ? ? layoutBox = this.findViewById(R.id.box);
? ? ? ? //初始化一個中間布局類
? ? ? ? Box box = new Box(this);
? ? ? ? //進行組合布局
? ? ? ? layoutBox.addView(box);
? ? }
? ? /**
? ? ?* 獲取標題布局
? ? ?* */
? ? public void getButtons(){
? ? ? ? //獲取總布局中的底部布局
? ? ? ? layoutButton = this.findViewById(R.id.button);
? ? ? ? //初始化一個底部布局類
? ? ? ? Buttons buttons = new Buttons(this);
? ? ? ? //進行組合布局
? ? ? ? layoutButton.addView(buttons);
? ? }
}其相對的主要布局文件如下:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? tools:context=".MainActivity"> ? ? <RelativeLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent"> ? ? ? ? <RelativeLayout ? ? ? ? ? ? android:id="@+id/title" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="60dp" ? ? ? ? ? ? /> ? ? ? ? <RelativeLayout ? ? ? ? ? ? android:id="@+id/box" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="590dp" ? ? ? ? ? ? android:layout_above="@+id/button" ? ? ? ? ? ? android:layout_below="@+id/title" /> ? ? ? ? <RelativeLayout ? ? ? ? ? ? android:id="@+id/button" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="80dp" ? ? ? ? ? ? android:layout_alignParentBottom="true" ? ? ? ? ? ? /> ? ? </RelativeLayout> </androidx.constraintlayout.widget.ConstraintLayout>
2. 首先其他的一些組合布局的類以及其相對布局文件
1)、標題布局
/**
?* author:LZH
?* Date: 2020/6/9
?* ClassName:Title
?* Intruduce:標題布局類
?*/
public class Titles extends RelativeLayout {
? ? public Titles(Context context) {
? ? ? ? super(context);
? ? ? ? View.inflate(context, R.layout.activity_title,this);
? ? }
? ? public Titles(Context context, AttributeSet attrs) {
? ? ? ? super(context, attrs);
? ? ? ? View.inflate(context, R.layout.activity_title,this);
? ? }
? ? public Titles(Context context, AttributeSet attrs, int defStyleAttr) {
? ? ? ? super(context, attrs, defStyleAttr);
? ? ? ? View.inflate(context, R.layout.activity_title,this);
? ? }
}布局文件:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="60dp" ? ? tools:context=".MainActivity"> ? ? <RelativeLayout ? ? ? ? android:id="@+id/title" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="60dp" ? ? ? ? tools:ignore="MissingConstraints" ? ? ? ? android:background="#CCFF00"> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="120dp" ? ? ? ? ? ? android:layout_height="30dp" ? ? ? ? ? ? android:layout_centerInParent="true" ? ? ? ? ? ? android:textSize="20dp" ? ? ? ? ? ? android:text="這個是標題" ? ? ? ? ? ? /> ? ? </RelativeLayout> </androidx.constraintlayout.widget.ConstraintLayout>
2)、中間布局
/**
?* author:LZH
?* Date: 2020/6/9
?* ClassName:Box
?* Intruduce:中間布局類
?*/
public class Box extends RelativeLayout {
? ? public Box(Context context) {
? ? ? ? super(context);
? ? ? ? View.inflate(context, R.layout.activity_box,this);
? ? }
? ? public Box(Context context, AttributeSet attrs) {
? ? ? ? super(context, attrs);
? ? ? ? View.inflate(context, R.layout.activity_box,this);
? ? }
? ? public Box(Context context, AttributeSet attrs, int defStyleAttr) {
? ? ? ? super(context, attrs, defStyleAttr);
? ? ? ? View.inflate(context, R.layout.activity_box,this);
? ? }
}布局文件:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? tools:context=".MainActivity"> ? ? <RelativeLayout ? ? ? ? android:id="@+id/box" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="590dp" ? ? ? ? tools:ignore="MissingConstraints" ? ? ? ? android:background="#6600"> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="150dp" ? ? ? ? ? ? android:layout_height="590dp" ? ? ? ? ? ? android:layout_marginTop="450dp" ? ? ? ? ? ? android:layout_centerInParent="true" ? ? ? ? ? ? android:textSize="20dp" ? ? ? ? ? ? android:text="這個是中間布局" ? ? ? ? ? ? /> ? ? </RelativeLayout> </androidx.constraintlayout.widget.ConstraintLayout>
3)、底部布局
/**
?* author:LZH
?* Date: 2020/6/9
?* ClassName:Button
?* Intruduce:底部布局類
?*/
public class Buttons extends RelativeLayout {
? ? public Buttons(Context context) {
? ? ? ? super(context);
? ? ? ? View.inflate(context, R.layout.activity_button,this);
? ? }
? ? public Buttons(Context context, AttributeSet attrs) {
? ? ? ? super(context, attrs);
? ? ? ? View.inflate(context, R.layout.activity_button,this);
? ? }
? ? public Buttons(Context context, AttributeSet attrs, int defStyleAttr) {
? ? ? ? super(context, attrs, defStyleAttr);
? ? ? ? View.inflate(context, R.layout.activity_button,this);
? ? }
}布局文件:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? tools:context=".MainActivity"> ? ? <RelativeLayout ? ? ? ? android:id="@+id/box" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="80dp" ? ? ? ? tools:ignore="MissingConstraints" ? ? ? ? android:background="#ccff"> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="150dp" ? ? ? ? ? ? android:layout_height="30dp" ? ? ? ? ? ? android:layout_centerInParent="true" ? ? ? ? ? ? android:textSize="20dp" ? ? ? ? ? ? android:text="這個是底部布局" ? ? ? ? ? ? /> ? ? </RelativeLayout> </androidx.constraintlayout.widget.ConstraintLayout>
效果圖:

總結,其中第一中方法是真正的跳轉方法,而第二中相對于一種組合布局,前者要用到兩個或者多個Activity的子類,而后者只需要一個MainActivity。另外,在存在多個Activity的子類時需要設置多個入口,也就是
<activity android:name=".NextActivity"/>
其中,“.”后面是你Activity的子類的名字。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android依據(jù)名字通過反射獲取在drawable中的圖片
依據(jù)圖片的名字,通過反射獲取其在drawable中的ID,在根據(jù)此ID顯示圖片,具體實現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06
Flutter監(jiān)聽當前頁面可見與隱藏狀態(tài)的代碼詳解
文章介紹了如何在Flutter中使用路由觀察者來監(jiān)聽應用進入前臺或后臺狀態(tài)以及頁面的顯示和隱藏,并通過代碼示例講解的非常詳細,需要的朋友可以參考下2025-03-03
Android應用中炫酷的橫向和環(huán)形進度條的實例分享
這篇文章主要介紹了Android應用中炫酷的橫向和圓形進度條的實例分享,文中利用了一些GitHub上的插件進行改寫,也是一片很好的二次開發(fā)教學,需要的朋友可以參考下2016-04-04
Android Studio 3.5版本JNI生成SO文件詳解
這篇文章主要介紹了Android Studio 3.5版本JNI生成SO文件詳解,想了解JNI的同學,可以參考下2021-04-04

