欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)

 更新時(shí)間:2022年06月16日 14:34:12   作者:聰明的小呆呆  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論