Android實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
本文實(shí)例為大家分享了Android實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的具體代碼,供大家參考,具體內(nèi)容如下
一. Android實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)有兩種方式,一種為.MainActivity跳轉(zhuǎn);第二種是Relatelayout布局跳轉(zhuǎn),首先看第一種方式
1. MainActivity區(qū)域設(shè)置
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); ? ? ? ? //按鈕進(jìn)行監(jiān)聽(tīng) ? ? ? ? button.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? //監(jiān)聽(tīng)按鈕,如果點(diǎn)擊,就跳轉(zhuǎn) ? ? ? ? ? ? ? ? Intent intent = new Intent(); ? ? ? ? ? ? ? ? //前一個(gè)(MainActivity.this)是目前頁(yè)面,后面一個(gè)是要跳轉(zhuǎn)的下一個(gè)頁(yè)面 ? ? ? ? ? ? ? ? intent.setClass(MainActivity.this,NextActivity.class); ? ? ? ? ? ? ? ? startActivity(intent); ? ? ? ? ? ? } ? ? ? ? }); ? ? } }
2. 這是下一個(gè)頁(yè)面 的設(shè)置
public class NextActivity extends AppCompatActivity { ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? //這個(gè)是獲取布局文件的,這里是你下一個(gè)頁(yè)面的布局文件 ? ? ? ? setContentView(R.layout.activity_next); ? ? } }
3. 這是第一個(gè)頁(yè)面的布局文件
<?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="這是第一個(gè)頁(yè)面!" ? ? ? ? ? ? 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="跳轉(zhuǎn)" ? ? ? ? ? ? android:layout_centerHorizontal="true" ? ? ? ? ? ? android:layout_below="@+id/one" ? ? ? ? ? ? /> ? ? </RelativeLayout> </androidx.constraintlayout.widget.ConstraintLayout>
4. 這是第二個(gè)頁(yè)面的布局文件
<?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="這是第二個(gè)頁(yè)面!" ? ? ? ? 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配置加上第二個(gè)頁(yè)面的入口
6. 效果圖
二. 第二種方式是通過(guò)控制Java布局文件進(jìn)行布局組合
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() { ? ? ? ? //獲取標(biāo)題布局 ? ? ? ? getTitles(); ? ? ? ? //獲取中間布局 ? ? ? ? getBoxs(); ? ? ? ? //獲取底部布局 ? ? ? ? getButtons(); ? ? } ? ? /** ? ? ?* 獲取標(biāo)題布局 ? ? ?* */ ? ? public void getTitles(){ ? ? ? ? //獲取總布局中的標(biāo)題布局 ? ? ? ? layoutTitle = this.findViewById(R.id.title); ? ? ? ? //初始化一個(gè)標(biāo)題布局類(lèi) ? ? ? ? Titles title = new Titles(this); ? ? ? ? //進(jìn)行組合布局 ? ? ? ? layoutTitle.addView(title); ? ? } ? ? /** ? ? ?* 獲取標(biāo)題布局 ? ? ?* */ ? ? public void getBoxs(){ ? ? ? ? //獲取總布局中的中間布局 ? ? ? ? layoutBox = this.findViewById(R.id.box); ? ? ? ? //初始化一個(gè)中間布局類(lèi) ? ? ? ? Box box = new Box(this); ? ? ? ? //進(jìn)行組合布局 ? ? ? ? layoutBox.addView(box); ? ? } ? ? /** ? ? ?* 獲取標(biāo)題布局 ? ? ?* */ ? ? public void getButtons(){ ? ? ? ? //獲取總布局中的底部布局 ? ? ? ? layoutButton = this.findViewById(R.id.button); ? ? ? ? //初始化一個(gè)底部布局類(lèi) ? ? ? ? Buttons buttons = new Buttons(this); ? ? ? ? //進(jìn)行組合布局 ? ? ? ? layoutButton.addView(buttons); ? ? } }
其相對(duì)的主要布局文件如下:
<?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. 首先其他的一些組合布局的類(lèi)以及其相對(duì)布局文件
1)、標(biāo)題布局
/** ?* author:LZH ?* Date: 2020/6/9 ?* ClassName:Title ?* Intruduce:標(biāo)題布局類(lèi) ?*/ 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="這個(gè)是標(biāo)題" ? ? ? ? ? ? /> ? ? </RelativeLayout> </androidx.constraintlayout.widget.ConstraintLayout>
2)、中間布局
/** ?* author:LZH ?* Date: 2020/6/9 ?* ClassName:Box ?* Intruduce:中間布局類(lèi) ?*/ 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="這個(gè)是中間布局" ? ? ? ? ? ? /> ? ? </RelativeLayout> </androidx.constraintlayout.widget.ConstraintLayout>
3)、底部布局
/** ?* author:LZH ?* Date: 2020/6/9 ?* ClassName:Button ?* Intruduce:底部布局類(lèi) ?*/ 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="這個(gè)是底部布局" ? ? ? ? ? ? /> ? ? </RelativeLayout> </androidx.constraintlayout.widget.ConstraintLayout>
效果圖:
總結(jié),其中第一中方法是真正的跳轉(zhuǎn)方法,而第二中相對(duì)于一種組合布局,前者要用到兩個(gè)或者多個(gè)Activity的子類(lèi),而后者只需要一個(gè)MainActivity。另外,在存在多個(gè)Activity的子類(lèi)時(shí)需要設(shè)置多個(gè)入口,也就是
<activity android:name=".NextActivity"/>
其中,“.”后面是你Activity的子類(lèi)的名字。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android studio按鈕點(diǎn)擊頁(yè)面跳轉(zhuǎn)詳細(xì)步驟
- Android?Studio實(shí)現(xiàn)簡(jiǎn)單頁(yè)面跳轉(zhuǎn)的詳細(xì)教程
- Android跳轉(zhuǎn)系統(tǒng)設(shè)置Settings的各個(gè)界面詳解
- Android開(kāi)發(fā)Intent跳轉(zhuǎn)傳遞list集合實(shí)現(xiàn)示例
- Android Studio實(shí)現(xiàn)注冊(cè)頁(yè)面跳轉(zhuǎn)登錄頁(yè)面的創(chuàng)建
- android app跳轉(zhuǎn)應(yīng)用商店實(shí)現(xiàn)步驟
相關(guān)文章
android 把float轉(zhuǎn)換成Int的實(shí)例講解
今天小編就為大家分享一篇android 把float轉(zhuǎn)換成Int的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Android編程出現(xiàn)Button點(diǎn)擊事件無(wú)效的解決方法示例
這篇文章主要介紹了Android編程出現(xiàn)Button點(diǎn)擊事件無(wú)效的解決方法,結(jié)合實(shí)例形式分析了Android編程中出現(xiàn)Button點(diǎn)擊事件無(wú)效的原因及相關(guān)的解決方法,需要的朋友可以參考下2018-02-02Android中使用DialogFragment編寫(xiě)對(duì)話框的實(shí)例教程
這篇文章主要介紹了Android中使用DialogFragment編寫(xiě)對(duì)話框的實(shí)例教程,DialogFragment也是一種Fragment,因而管理生命周期時(shí)比較給力,需要的朋友可以參考下2016-04-04OpenGL ES透視投影實(shí)現(xiàn)方法(四)
這篇文章主要為大家詳細(xì)介紹了OpenGL ES透視投影的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android自定義View之邊框文字、閃爍發(fā)光文字
這篇文章主要為大家詳細(xì)介紹了Android自定義View之邊框文字、閃爍發(fā)光文字,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android?獲取手機(jī)已安裝的應(yīng)用列表實(shí)現(xiàn)詳解
這篇文章主要介紹了Android?獲取手機(jī)已安裝的應(yīng)用列表的實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Android 動(dòng)態(tài)添加view或item并獲取數(shù)據(jù)的實(shí)例
下面小編就為大家?guī)?lái)一篇Android 動(dòng)態(tài)添加view或item并獲取數(shù)據(jù)的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10Android ContentResolver使用說(shuō)明
這篇文章主要介紹了Android ContentResolver使用說(shuō)明,需要的朋友可以參考下2016-01-01初學(xué)Android之網(wǎng)絡(luò)封裝實(shí)例
大家好,本篇文章主要講的是初學(xué)Android之網(wǎng)絡(luò)封裝實(shí)例,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12