網站首頁 編程語言 正文
本文實例為大家分享了Android實現頁面跳轉的具體代碼,供大家參考,具體內容如下
一. Android實現頁面跳轉有兩種方式,一種為.MainActivity跳轉;第二種是Relatelayout布局跳轉,首先看第一種方式
1. MainActivity區域設置
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);
? ? ? ? //按鈕進行監聽
? ? ? ? button.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? //監聽按鈕,如果點擊,就跳轉
? ? ? ? ? ? ? ? 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的子類的名字。
原文鏈接:https://blog.csdn.net/lzh623015455/article/details/106633605
相關推薦
- 2024-03-04 echarts 柱狀圖,單獨一根柱子根據條件改變顏色
- 2022-10-17 Nginx中root與alias區別講解_nginx
- 2022-09-19 ASP.NET?Core模仿中間件方式實現列表過濾功能_實用技巧
- 2022-08-15 centos7 redis5安裝
- 2022-12-23 Mariadb數據庫主從復制同步配置過程實例_mariadb
- 2022-03-20 .NET?6開發TodoList應用之實現接口請求驗證_實用技巧
- 2022-11-09 ASP.NET?MVC視圖頁使用jQuery傳遞異步數據的幾種方式詳解_實用技巧
- 2023-11-13 【云原生】docker設置非root用戶使用權限的方法
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支