在學(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).
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名注冊(cè)、網(wǎng)絡(luò)空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、陵城網(wǎng)站維護(hù)、網(wǎng)站推廣。
使用顯示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)聽器 @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í)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
名稱欄目:Android使用Intent顯示實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
當(dāng)前鏈接:http://jinyejixie.com/article26/ijghjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)站制作、面包屑導(dǎo)航、網(wǎng)站設(shè)計(jì)、小程序開發(fā)、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)