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。
那要如何解决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。
相关文章推荐
- Android中一个Activity调用另一个Activity — Intent对象的使用
- Android中级教程(二)之调用另一个Activity---Intent对象的使用!
- Ex3_9 调用另一个Activity —— Intent对象的使用
- Android中级教程(二)之调用另一个Activity---Intent对象的使用!
- 使用intent由一个Activity启动另外一个Activity
- 调用另一个Activity----Intent对象的使用
- 调用另一个Activity---Intent对象的使用
- Andorid 中调用另一个activity-intent 对象的使用
- Android中级教程(二)之调用另一个Activity---Intent对象的使用!
- Android中级教程(二)之调用另一个Activity---Intent对象的使用!
- 循环使用summit调用另外一个报表时,注意事项
- 如何在非Activity类中,跳转到另外一个Activity类(context.startActivity(intent); )
- 定义一个交通工具(Vehicle)的类,在测试类Vehicle中的main()中实例化一个交通工具对象,通过方法给它初始化speed,size的值,并打印出来。另外,调用加速,减速的方法对速度进行改变
- 5.编写一个程序,有两个类,其中类MAX中具有两个方法,方法名都是max,一个方法能够比较两个数的大小,另外一个方法能够比较三个数的大小。在另外一个类中创建对象,调用这两个方法,分别输出两组数:2、6
- 使用Intent传递对象--Android学习笔记4
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- 在一个apk中调用另外一个apk中的activity
- 在一个Activity中调用另外一个Activity的变量或方法
- android 开发手记 activity 之间数据的传递 intent 对象的使用
- 当使用System,out.println()打印一个对象是自动调用toString方法