Android 程式開發:(二)使用意圖 —— 2.1 連結Activities

來源:互聯網
上載者:User

  在一個Android應用中可以包含零個或多個Acivity。當你的應用中包含多個Activity時,通常要在各個Activity中間跳轉。在Android中,完成這些操作需要使用Intent的組件。
 
    理解這個既重要又抽象概念的最好辦法,就是嘗試一下。下面的例子展示如何在兩個Activity之間跳轉。
 
    1.建立一個名為UsingIntent的工程。
 
    2.建立兩個Activity:UsingIntentActivity和SecondActivitty。
 
    3.AndroidManifest.xml中的代碼。
 
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="net.horsttnann.UsingIntent" 
    android:versionCode="1" 
    android:versionName="1.0" > 
 
    <uses-sdk android:minSdkVersion="14" /> 
 
    <application 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" > 
        <activity 
            android:name=".UsingIntentActivity" 
            android:label="@string/app_name" > 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
        <activity 
            android:name=".SecondActivity" 
            android:label="Second Activity" > 
            <intent-filter> 
                <action android:name="net.horsttnann.SecondActivity" /> 
 
                <category android:name="android.intent.category.DEFAULT" /> 
            </intent-filter> 
        </activity> 
    </application> 
 
</manifest> 
    4.在res/layout檔案夾下,建立一個叫secondactivity.xml的檔案。<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="This is the Second Activity!" /> 
 
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Please enter your name" /> 
 
    <EditText 
        android:id="@+id/txt_username" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" /> 
 
    <Button 
        android:id="@+id/btn_OK" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:onClick="onClick" 
        android:text="OK" /> 
 
</LinearLayout> 
    5.SecondActivity中的代碼。package net.horsttnann.UsingIntent; 
 
import android.app.Activity; 
import android.os.Bundle; 
 
public class SecondActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.secondactivity); 
    } 
} 
    6.main.xml中的代碼。<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <Button 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:onClick="onClick" 
        android:text="Display second activity" /> 
 
</LinearLayout> 
    7.UsingIntentActivity中的代碼。package net.horsttnann.UsingIntent; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
 
public class UsingIntentActivity extends Activity { 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
    } 
 
    public void onClick(View view) { 
        startActivity(new Intent("net.horsttnann.SecondActivity")); 
    } 
 
} 
    8.按F11調試。 www.2cto.com
 
 
 
:
 


提示:
 
    Intent-Filter中的anction屬性盡量使用反轉網域名稱,這樣就能減少被其他使用相同action的程式啟動的幾率。  
 
 
    Intent-Filter中的category屬性是android.intent.category.DEFAULT。只有添加了這個屬性,這個Activity才能被另外一個Activity使用startActivity()方法來開啟。
 
    還有另外一種啟動Activity的方法,但如果想使用這個方法,就必須確保這兩個Activity在同一個工程下面。
 
 
startActivity(new Intent(this, SecondActivity.class)); 
 
 
 作者 manoel的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.