展会信息港展会大全

Android新手入门教程(八):使用Intents链接Activities
来源:互联网   发布日期:2016-01-14 09:43:20   浏览:1737次  

导读:上一篇:http://www.2cto.com/kf/201203/124600.html在一个Android应用中可以包含零个或多个Acivity。当你的应用中包含多个Activity时...

上一篇:http://www.2cto.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" />

</LinearLayout>

<?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中的代码。

[java] 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);

}

}

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中的代码。

[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" >

<Button

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:onClick="onClick"

android:text="Display second activity" />

</LinearLayout>

<?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中的代码。

[java] 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"));

}

}

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调试。

效果图:

提示:

Intent-Filter中的anction属性尽量使用反转域名,这样就能减少被其他使用相同action的程序启动的几率。

Intent-Filter中的category属性是android.intent.category.DEFAULT。只有添加了这个属性,这个Activity才能被另外一个Activity使用startActivity()方法来开启。

还有另外一种启动Activity的方法,但如果想使用这个方法,就必须确保这两个Activity在同一个工程下面。

[java]startActivity(new Intent(this, SecondActivity.class));

startActivity(new Intent(this, SecondActivity.class));

摘自horsttnann的专栏

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港