当前位置:操作系统 > 安卓/Android >>

Android新手入门教程(八):使用Intents链接Activities

上一篇:http://www.zzzyk.com/kf/201203/124600.html 

在一个Android应用中可以包含零个或多个Acivity。当你的应用中包含多个Activity时,通常要在各个Activity中间跳转。在Android中,完成这些操作需要使用Intent的组件。

    理解这个既重要又抽象概念的最好办法,就是尝试一下。下面的例子展示如何在两个Activity之间跳转。

    1.创建一个名为UsingIntent的工程。

    2.创建两个Activity:UsingIntentActivity和SecondActivitty。

    3.AndroidManifest.xml中的代码。


[java] <?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> 
<?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的文件。


[java] <?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" />&nb

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,