Android使用Intent顯示實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
在學(xué)習(xí)安卓的最初過(guò)程中我們學(xué)的都是最基本的一個(gè)活動(dòng),只有一個(gè)活動(dòng)的應(yīng)用也太簡(jiǎn)單了吧,沒(méi)錯(cuò)我們的最求應(yīng)該更高點(diǎn),不管你創(chuàng)建多少個(gè)活動(dòng),接下里我們介紹的這種方法能解決我們?cè)趧?chuàng)建活動(dòng)之間的跳轉(zhuǎn).
使用顯示Intent
剛?cè)腴T學(xué)習(xí)Android的小伙伴們已經(jīng)能很嫻熟的使用Android studio 創(chuàng)建一個(gè)項(xiàng)目了,接下來(lái)我把我自己創(chuàng)建的目錄先展示下
首先創(chuàng)建一個(gè)名叫TestIntent的project然后在main--java下面創(chuàng)建了2個(gè)類分別是FirstActivity和MainActivity,其次再是創(chuàng)建2個(gè)布局分別是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="無(wú)返回結(jié)果的頁(yè)面跳轉(zhuǎn)" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button1" /> <Button android:text="有結(jié)果的頁(yè)面跳轉(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="這是第二個(gè)界面" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button" /> </LinearLayout>
上面2個(gè)就是我們基本的布局,然后就是活動(dòng)里面需要編寫的邏輯了首先是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)聽(tīng)器 @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this,FirstActivity.class); startActivity(intent); } }); } }
接下來(lái)我們的重點(diǎn)是Intent intent = new Intent(MainActivity.this,FirstActivity.class);
Intent有多個(gè)構(gòu)造函數(shù)的重載,其中一個(gè)是Intent(Context packageContext,Class<?>cls).這個(gè)構(gòu)造函數(shù)接受兩個(gè)參數(shù),第一個(gè)參數(shù)Context要求提供一個(gè)啟動(dòng)活動(dòng)的上下文,第二個(gè)參數(shù)Class則是指定想要啟動(dòng)的目標(biāo)活動(dòng),通過(guò)這個(gè)構(gòu)造函數(shù)就可以構(gòu)建出Intent的意圖,,但是我們?cè)撛趺词褂肐ntent呢?Activity提供了一個(gè)startActivity()方法,這個(gè)方法是專門啟動(dòng)活動(dòng)的,他接收一個(gè)Intent參數(shù),這里我們把intent傳入進(jìn)去就可以啟動(dòng)活動(dòng)了
這里MainActivity.this作為上下文,FirstActivity.class作為目標(biāo)活動(dòng),然后通過(guò)startActivity(intent)啟動(dòng)活動(dòng)
下面這個(gè)是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); } }
當(dāng)然了我們還有一個(gè)重要的地方需要去修改下那就是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>
這段代碼主要是首先啟動(dòng)哪個(gè)活動(dòng),因?yàn)槲覀兪紫葐?dòng)的是MainActivity這個(gè)活動(dòng)所以在那里添加這段代碼,第二個(gè)活動(dòng)不需要去添加這段代碼
接下來(lái)我們啟動(dòng)模擬器如圖
點(diǎn)擊第一個(gè)按鈕然后就可以跳轉(zhuǎn)到第二個(gè)界面
可以看到我們已經(jīng)成功啟動(dòng)了第二個(gè)活動(dòng),這就是我們Intent顯示實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn).
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Studio實(shí)現(xiàn)注冊(cè)頁(yè)面跳轉(zhuǎn)登錄頁(yè)面的創(chuàng)建
- Android實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的全過(guò)程記錄
- Android使用Intent隱式實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
- Android Intent實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的兩種方法
- Android Intent實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的方法示例
- Android 實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
- Android使用Circular Reveal動(dòng)畫讓頁(yè)面跳轉(zhuǎn)更炫酷
- Android編程中Intent實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)功能詳解
- Android Activity中使用Intent實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)與參數(shù)傳遞的方法
- Android實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
相關(guān)文章
Android實(shí)現(xiàn)二級(jí)購(gòu)物車的全選加反選、總價(jià)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)二級(jí)購(gòu)物車的全選加反選、總價(jià)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Android實(shí)現(xiàn)圖片反轉(zhuǎn)、翻轉(zhuǎn)、旋轉(zhuǎn)、放大和縮小
這篇文章主要介紹了Android實(shí)現(xiàn)圖片反轉(zhuǎn)、翻轉(zhuǎn)、旋轉(zhuǎn)、放大和縮小的相關(guān)代碼,需要的朋友可以參考下2015-09-09Android開(kāi)發(fā)時(shí)盡管已root但是ddms還是沒(méi)有data路徑怎么辦
這篇文章主要介紹了Android開(kāi)發(fā)時(shí)盡管已root但是ddms還是沒(méi)有data路徑怎么辦的相關(guān)資料,需要的朋友可以參考下2015-12-12Android Socket通信的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android Socket通信的簡(jiǎn)單實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09SurfaceView播放視頻發(fā)送彈幕并實(shí)現(xiàn)滾動(dòng)歌詞
這篇文章主要為大家詳細(xì)介紹了SurfaceView播放視頻發(fā)送彈幕并實(shí)現(xiàn)滾動(dòng)歌詞,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11Android使用注解進(jìn)行代碼檢查的實(shí)現(xiàn)方法
這篇文章主要介紹了Android如何使用注解進(jìn)行代碼檢查,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09android中RecycleView添加下滑到底部的監(jiān)聽(tīng)示例
本篇文章主要介紹了android中RecycleView添加下滑到底部的監(jiān)聽(tīng)示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03