官术网_书友最值得收藏!

2.2.3 Activity的跳轉(zhuǎn)方法

所有頁(yè)面都會(huì)涉及多個(gè)頁(yè)面之間的跳轉(zhuǎn),和前端的HTML跳轉(zhuǎn)一樣,只不過(guò)移動(dòng)端頁(yè)面的跳轉(zhuǎn)是Activity之間的跳轉(zhuǎn),并且Android App的跳轉(zhuǎn)也是可以傳遞參數(shù)的。

下面我們先實(shí)現(xiàn)一個(gè)從一個(gè)Activity跳轉(zhuǎn)到另一個(gè)Activity的操作,實(shí)現(xiàn)這個(gè)操作要4步。

第1步,新建兩個(gè)Activity。本書前面介紹過(guò),但凡有界面的地方就一定有Activity,況且我們要實(shí)現(xiàn)的功能還是一個(gè)Activity跳轉(zhuǎn)到另一個(gè)Activity,所以第一步要新建兩個(gè)Activity。在項(xiàng)目的com.example.chenchen.book下創(chuàng)建兩個(gè)文件,一個(gè)是AActivity.java,另一個(gè)是BActivity.java,如代碼清單2-8、代碼清單2-9所示。

代碼清單2-8 AActivity代碼實(shí)現(xiàn)

package com.example.chenchen.book;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class AActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
    }
}

代碼清單2-9 BActivity代碼實(shí)現(xiàn)

package com.example.chenchen.book;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class BActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
    }
}

代碼清單2-8、代碼清單2-9就是我們新建一個(gè)Android項(xiàng)目時(shí),默認(rèn)創(chuàng)建的Activity的代碼。大家看到代碼清單2-8和代碼清單2-9中都有一行代碼執(zhí)行了setContentView()的函數(shù),setContentView()函數(shù)就是我們之前提過(guò)的設(shè)置App的界面文件。

第2步,我們需要編寫兩個(gè)xml文件,作為AActivity和BActivity的界面。AActivity對(duì)應(yīng)的界面文件名字叫activity_a.xml,具體的代碼實(shí)現(xiàn)見(jiàn)代碼清單2-10。

代碼清單2-10 activity_a.xml代碼實(shí)現(xiàn)

<?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我是AActivity"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:layout_width="150dp"
            android:layout_height="30dp"
            android:text="跳轉(zhuǎn)到B"
            android:id="@+id/toB"
            />

</android.support.constraint.ConstraintLayout>

activity_a.xml中的部分代碼之前配置Activity時(shí)介紹過(guò)。代碼清單2-10中需要關(guān)注的是<Button>標(biāo)簽,因?yàn)楫?dāng)我們從AActivity跳轉(zhuǎn)到BActivity的時(shí)候需要點(diǎn)擊這個(gè)按鈕觸發(fā)跳轉(zhuǎn)事件,另外在<Button>標(biāo)簽中出現(xiàn)了之前沒(méi)有見(jiàn)過(guò)的屬性,就是id。這個(gè)屬性聲明了<Button>標(biāo)簽的id為toB,后續(xù)我們?cè)贏ctivity中編寫邏輯代碼的時(shí)候,可以通過(guò)查找界面的xml中id為toB的標(biāo)簽來(lái)獲取這個(gè)<Button>標(biāo)簽。(id在綁定事件的時(shí)候比較常用。)

相比之下,activity_b.xml的代碼就簡(jiǎn)單一點(diǎn),就只有一個(gè)<TextView>標(biāo)簽,用于標(biāo)記展示的Activity是AActivity還是BActivity,具體如代碼清單2-11所示。

代碼清單2-11 activity_b.xml代碼實(shí)現(xiàn)

<?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我是BActivity"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

第3步,為AActivity界面中的按鈕添加點(diǎn)擊事件,具體如代碼清單2-12所示。在代碼清單2-12中,先找到要綁定事件的標(biāo)簽,即之前設(shè)置id屬性的那個(gè)按鈕。在Android編程過(guò)程中,大多數(shù)xml界面文件中的標(biāo)簽獲取都是通過(guò)findViewByIdid函數(shù)來(lái)進(jìn)行獲取,然后把對(duì)應(yīng)屬性的id當(dāng)作參數(shù)傳入到函數(shù)中就可以了。

之后,通過(guò)setOnClickListener函數(shù)綁定這個(gè)按鈕的點(diǎn)擊事件,并在點(diǎn)擊事件里加入跳轉(zhuǎn)的功能代碼。我們通過(guò)創(chuàng)建Intent對(duì)象的方式來(lái)實(shí)現(xiàn),Intent初始化的時(shí)候會(huì)傳入兩個(gè)參數(shù),第1個(gè)參數(shù)為當(dāng)前AActivity的上下文,也就是AActivity.this屬性,第2個(gè)參數(shù)是目標(biāo)Activity的class屬性,也就是BActivity.class。啟動(dòng)跳轉(zhuǎn)的操作通過(guò)AActivity.this.startActivity()函數(shù)實(shí)現(xiàn),把我們剛剛初始化的Intent傳入AActivity.this.startActivity()函數(shù)中,就能實(shí)現(xiàn)界面從AActivity跳轉(zhuǎn)到BActivity。

代碼清單2-12 AActivity.java代碼實(shí)現(xiàn)

package com.example.chenchen.book;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class AActivity extends AppCompatActivity {
    AActivity mContext = this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
        Button button = findViewById(R.id.toB);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AActivity.this,BActivity.class);
                mContext.startActivity(intent);
            }
        });
    }
}

但是在這個(gè)時(shí)候,并不代表AActivity跳轉(zhuǎn)BActivity這個(gè)功能就做完了,因?yàn)槲覀冞€沒(méi)有更改啟動(dòng)時(shí)的Activity配置。該配置在2.2.2節(jié)提到的文件AndroidMani-fest.xml中,所以第4步需要把intent-filter標(biāo)簽以及里面的嵌套標(biāo)簽用AActivity的<Activity>標(biāo)簽包裹起來(lái),具體修改代碼如代碼清單2-13所示。

代碼清單2-13 修改App啟動(dòng)入口為AActivity

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.chenchen.book">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">

        </activity>
        <activity android:name=".AActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".BActivity">
        </activity>
    </application>

</manifest>}

當(dāng)我們把上面這些配置文件配置完成之后,在啟動(dòng)App時(shí)就默認(rèn)會(huì)啟動(dòng)AActivity,啟動(dòng)之后的界面如圖2-10所示。

圖2-10 App啟動(dòng)時(shí)的AActivity界面

在圖2-10界面中點(diǎn)擊“跳轉(zhuǎn)到B”選項(xiàng)后,我們就會(huì)看到圖2-11中的界面。到這里,Activity之間的基礎(chǔ)跳轉(zhuǎn)就完成了。

圖2-11 AActivity跳轉(zhuǎn)到BActivity之后的界面

主站蜘蛛池模板: 兴隆县| 安化县| 凤冈县| 筠连县| 池州市| 楚雄市| 香河县| 淅川县| 台山市| 横峰县| 都昌县| 凯里市| 台江县| 余干县| 潜江市| 门源| 杭州市| 福海县| 息烽县| 茶陵县| 巧家县| 屏南县| 白山市| 清新县| 晋江市| 郎溪县| 叙永县| 托里县| 太仆寺旗| 怀仁县| 宜丰县| 呈贡县| 新河县| 平原县| 东莞市| 贡嘎县| 和平区| 含山县| 子洲县| 滦南县| 陵水|