Android使用Intent顯示實現(xiàn)頁面跳轉(zhuǎn)
在學習安卓的最初過程中我們學的都是最基本的一個活動,只有一個活動的應用也太簡單了吧,沒錯我們的最求應該更高點,不管你創(chuàng)建多少個活動,接下里我們介紹的這種方法能解決我們在創(chuàng)建活動之間的跳轉(zhuǎn).
使用顯示Intent
剛?cè)腴T學習Android的小伙伴們已經(jīng)能很嫻熟的使用Android studio 創(chuàng)建一個項目了,接下來我把我自己創(chuàng)建的目錄先展示下

首先創(chuàng)建一個名叫TestIntent的project然后在main--java下面創(chuàng)建了2個類分別是FirstActivity和MainActivity,其次再是創(chuàng)建2個布局分別是activity_main.xml 和first_layout.xml
現(xiàn)在我將這創(chuàng)建好的布局代碼展示下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" tools:context="com.example.testintent.MainActivity"> <Button android:text="無返回結(jié)果的頁面跳轉(zhuǎn)" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button1" /> <Button android:text="有結(jié)果的頁面跳轉(zhuǎn)" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button2" /> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="初始界面" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:text="這是第二個界面" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button" /> </LinearLayout>
上面2個就是我們基本的布局,然后就是活動里面需要編寫的邏輯了首先是MainActivity
package com.example.testintent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button bt;//初始化控件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(new View.OnClickListener() {//創(chuàng)建監(jiān)聽器
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,FirstActivity.class);
startActivity(intent);
}
});
}
}
接下來我們的重點是Intent intent = new Intent(MainActivity.this,FirstActivity.class);
Intent有多個構(gòu)造函數(shù)的重載,其中一個是Intent(Context packageContext,Class<?>cls).這個構(gòu)造函數(shù)接受兩個參數(shù),第一個參數(shù)Context要求提供一個啟動活動的上下文,第二個參數(shù)Class則是指定想要啟動的目標活動,通過這個構(gòu)造函數(shù)就可以構(gòu)建出Intent的意圖,,但是我們該怎么使用Intent呢?Activity提供了一個startActivity()方法,這個方法是專門啟動活動的,他接收一個Intent參數(shù),這里我們把intent傳入進去就可以啟動活動了
這里MainActivity.this作為上下文,FirstActivity.class作為目標活動,然后通過startActivity(intent)啟動活動
下面這個是FirstActivity里面的代碼
package com.example.testintent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class FirstActivity extends AppCompatActivity {
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
}
}
當然了我們還有一個重要的地方需要去修改下那就是AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.testintent"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".FirstActivity" /> </application> </manifest>
這里面需要注意的是
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
這段代碼主要是首先啟動哪個活動,因為我們首先啟動的是MainActivity這個活動所以在那里添加這段代碼,第二個活動不需要去添加這段代碼
接下來我們啟動模擬器如圖

點擊第一個按鈕然后就可以跳轉(zhuǎn)到第二個界面

可以看到我們已經(jīng)成功啟動了第二個活動,這就是我們Intent顯示實現(xiàn)頁面跳轉(zhuǎn).
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android Studio實現(xiàn)注冊頁面跳轉(zhuǎn)登錄頁面的創(chuàng)建
- Android實現(xiàn)頁面跳轉(zhuǎn)的全過程記錄
- Android使用Intent隱式實現(xiàn)頁面跳轉(zhuǎn)
- Android Intent實現(xiàn)頁面跳轉(zhuǎn)的兩種方法
- Android Intent實現(xiàn)頁面跳轉(zhuǎn)的方法示例
- Android 實現(xiàn)頁面跳轉(zhuǎn)
- Android使用Circular Reveal動畫讓頁面跳轉(zhuǎn)更炫酷
- Android編程中Intent實現(xiàn)頁面跳轉(zhuǎn)功能詳解
- Android Activity中使用Intent實現(xiàn)頁面跳轉(zhuǎn)與參數(shù)傳遞的方法
- Android實現(xiàn)頁面跳轉(zhuǎn)
相關(guān)文章
Android高級組件ImageSwitcher圖像切換器使用方法詳解
這篇文章主要為大家詳細介紹了Android高級組件ImageSwitcher圖像切換器的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Android通過記住密碼功能學習數(shù)據(jù)存儲類SharedPreferences詳解及實例
這篇文章主要通過“記住密碼”實例功能學習為大家介紹了Android數(shù)據(jù)存儲類SharedPreferences,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-03-03
Android 靜默方式實現(xiàn)批量安裝卸載應用程序的深入分析
本篇文章是對Android 靜默方式實現(xiàn)批量安裝卸載應用程序進行了詳細的分析介紹,需要的朋友參考下2013-06-06
Kotlin Service實現(xiàn)消息推送通知過程
這幾天分析了一下的啟動過程,于是乎,今天寫一下Service使用; 給我的感覺是它并不復雜,千萬不要被一坨一坨的代碼嚇住了,雖然彎彎繞繞不少,重載函數(shù)一個接著一個,就向走迷宮一樣,但只要抓住主線閱讀,很快就能找到出口2022-12-12

