您的位置:首页 > 移动开发 > Android开发

Android学习笔记(九)—— 调用另外一个Activity — Intent对象的使用

2012-11-16 15:44 531 查看
如果要转换的页面并不单只是背景,颜色或文字内容的不同,而是Activity的置换,那就不是单单改变Layout就能完成的,尤其是需要传递的变量不像网页可以通过Cookie或Session,在程序里要移交主控权到另外一个Activity,光靠先前的Layout技巧是办不到的。

那要如何解决Activity控制权的移交呢?在Android的程序设计中,可在主程序里使用startActivity()这个方法来调用另外一个Activity(主程序本身即是一个Activity),但当中的关键并不在startActivity这个方法,而是Intent这个特有的对象。Intent就如同其英文字义,是“想要”或“意图”之意,在主Activity当中,告诉程序自己试什么,并想要前往哪里,这就是Intent对象所处理的事了。在本范例并没有特别的Layout布局,而是直接在主Activity(Activity1)当中部署一个按钮,当单机按钮的同时,告诉主Activity前往Activity2,并在Activity2里创建一个回到Activity的按钮,本范例将利用此简易的程序描述,示范如何在一个Activity中调用另一个Activity的手法。

运行结果:



点击Button之后:



点击Button之后返回之前的Activity

操作步骤:

step1:新建Android项目EX03_09_IntentDemo

step2:res->values->添加color.xml

<?xml version="1.0" encoding="UTF-8"?>

<resources>

<color name="white">#FFFFFF</color>

<color name="black">#000000</color>

<color name="red">#FF0000</color>

<color name="darkgray">#A9A9A9</color>

<color name="lightskyblue">#87CEFA</color>

<color name="pink">#FFC0CB</color>

<color name="lightpink">#FFB6C1</color>

<color name="darkblue">#00008B</color>

</resources>


step3:res->layout->main.xml

为了凸显Activity间切换的效果,特别将两个Layout的背景及输出文字有所区别。在main.xml中定义其背景色为黑色,输出文字为“This isActivity1!”,文字颜色为红色。

<?xml version="1.0" encoding="UTF-8"?>

<AbsoluteLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@color/black">

<TextView

android:id="@+id/text1"

android:textSize="24sp"

android:textColor="@color/red"


android:layout_width="220dp"

android:layout_height="40dp"

android:layout_x="70dp"

android:layout_y="50dp"

android:text="This is Activity 1!"/>

<Button

android:id="@+id/button1"

android:textColor="@color/red"

android:layout_width="180dp"

android:layout_height="wrap_content"

android:layout_x="70dp"

android:layout_y="100dp"

android:text="Go to Activity2"/>

</AbsoluteLayout>

step4:res->layout->添加mylayout.xml

在mylayout.xml中定义其背景色为粉色,输出文字为“This is Activity 2!”,文字的颜色为蓝色。

<?xml version="1.0" encoding="UTF-8"?>

<AbsoluteLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@color/pink">

<TextView

android:id="@+id/text2"

android:textSize="24sp"

android:textColor="@color/darkblue"


android:layout_width="220dp"

android:layout_height="50dp"

android:layout_x="70dp"

android:layout_y="50dp"

android:text="This is Activity 2!"/>

<Button

android:id="@+id/button2"

android:textColor="@color/darkblue"


android:layout_width="180dp"

android:layout_height="wrap_content"

android:layout_x="70dp"

android:layout_y="100dp"

android:text="Go to Activity1"/>

</AbsoluteLayout>

step5:EX03_09_IntentDemo.java

主程序中加载的Layout为main.xml。屏幕上显示的是黑色背景的“This is Acitvity 1!”,在Button被单机时调用另外一个Activity(EX03_09_1_IntentDeo,参考AndroidMainfest.xml里的说明),并将主Activity关闭finish(),接着将主控权交给下一个Activity,即Activity2。

package com.example.ex03_09_intentdemo;

import android.os.Bundle;

import android.app.Activity;

import android.view.View;

import android.widget.Button;

import android.content.Intent;


public class EX03_09_IntentDemo
extends Activity {

/**Called when the activity is first created*/

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

/*载入main.xml Layout*/

setContentView(R.layout.main);

/*以findViewById()取得Button对象,并添加onClickListener*/

Button b1 = (Button)findViewById(R.id.button1);

b1.setOnClickListener(new Button.OnClickListener(){

public void onClick(View v){


/*new 一个Intent对象,并制定要启动的class*/

Intent intent = new Intent();

intent.setClass(EX03_09_IntentDemo.this,EX03_09_1_IntentDemo.class);


/*调用一个新的Activity*/

startActivity(intent);

EX03_09_IntentDemo.this.finish();


}

});

}

}

step6:EX03_09_1_IntentDemo.java

EX03_09_1_IntentDemo.java程序是第二个Activity的主程序,其加载的Layout为mylayout.xml,屏幕上所显示的是粉色背景的“This is Activity 2!”,当主Activity(Activity1)调用这个Activity(Activity2)后,同样为Button添加onClickListener(),使Button被单击时,重新调用Activity1(EX03_09_IntentDemo),并将Activity2(EX03_09_1_IntentDemo)关闭(finish())。

package com.example.ex03_09_intentdemo;

import android.app.Activity;

import android.os.Bundle;

import android.content.Intent;

import android.view.View;

import android.widget.Button;


public class EX03_09_1_IntentDemo
extends Activity{

/**Called when the activity is first created*/

@Override

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

/*载入mylayout.xml Layout*/

setContentView(R.layout.mylayout);

/*以findViewById()取得Button对象,并添加onClickListener*/

Button b2 = (Button)findViewById(R.id.button2);

b2.setOnClickListener(new Button.OnClickListener(){

public void onClick(View v){


/*new一个Intent对象,并制定要启动的class*/

Intent intent = new Intent();

intent.setClass(EX03_09_1_IntentDemo.this,EX03_09_IntentDemo.class);


/*调用一个新的Activity*/

startActivity(intent);

/*关闭原本的Activity*/

EX03_09_1_IntentDemo.this.finish();

}


});

}

}

step7:AndroidMainfest.xml

由于本范例中添加了一个Activity,所以必须在AndroidMainfest.xml中定义一个新的activity,并给予名称name,否则程序将无法编译运行。

<?xml version="1.0" encoding="UTF-8"?>

<manifest

xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.ex03_09_intentdemo"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="15" />

<application

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name=".EX03_09_IntentDemo"

android:label="@string/title_activity_ex03_09__intent_demo" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<activity android:name="EX03_09_1_IntentDemo"></activity>

</application>

</manifest>

扩展学习:

当系统中新添加Activity时,必须在AndroidManfest.xml里定义一个新的activity:

<activity android:name="EX03_09_1_IntentDemo"></activity>

否则系统将会因为找不到Activity而发生编译错误。

另外,当程序中出现两个以上的Activity时,系统要如何决定主程序是哪一支(entry point)呢?以本范例来说,AndroidMainfest.xml中Activity EX03_09_IntentDemo的定义如下:

<activity

android:name=".EX03_09_IntentDemo"

android:label="@string/title_activity_ex03_09__intent_demo" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

其中有一行为<category android:name="android.intent.category.LAUNCHER" />,这就代表程序启动时,会先运行EX03_09_IntentDemo这个Activity,而非EX03_09_1_IntentDemo。需要注意的是,这个参数必须要被定义,如果xml中没有一支Activity有设置这个参数,则程序将不会被运行。

此外,在两支Java程序中的最后一行都调用了finish()这个方法,它代表这个Activity已运作完毕,当系统接收到这个命令时,即会关闭此Activity,所以此时单机模拟器的返回(back)键,并不会回到上一个Activity的画面,如果要让模拟器的返回键有回上一页的效果,可以将此行程序注释掉。同理,当两个Activity在切换时,并非真的只有两个Activity在切换,而是在单击按钮时再重新调用起一个新的Activity。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐