您的当前位置:首页正文

Android中的Intent标准跳转应用

来源:化拓教育网
Intent标准跳转应用

Android中的Intent标准跳转应用

1) Intent无参数Activity跳转

大部分的Android应用程序都是有多个页面(即 Activity)组成,那么Activity之间的跳转就成为了一个最重要的操作,我们在Android中使用Intent对象来实现界面之间的跳转。Intent实现界面之间的跳转常用两种语法形式实现:

第一种:Intent intent = new Intent(源Activity.this, 目标Activity.class);

第二种:Intent intent = new Intent();

Intent.setClass(源Activity.this, 目标Activity.class);

➢ 应用案例

实现两个Activity之间的无参数跳转应用。

➢ 实现效果参考图

1

Intent标准跳转应用

➢ 局部关键实现代码

 Activity_first.xml布局文件对按钮的声明

android:id=\"@+id/btnGoto\"

android:layout_width=\"fill_parent\"

android:layout_height=\"wrap_content\" android:text=\"跳转至第2个Activity\"/>

 Activity_second.xml布局文件对按钮的声明

android:id=\"@+id/btnGoto\"

android:layout_width=\"fill_parent\"

android:layout_height=\"wrap_content\" android:text=\"跳转至第1个Activity\"/>

2

Intent标准跳转应用

 AndroidManifest.xml清单文件中对窗体的声明

android:name=\".FirstActivity\"

android:label=\"案例10.1 无参数跳转界面1\">

android:name=.SecondActivity\" android:label=\"案例10.1 无参数跳转界面2\"/>

 FirstActivty.java中【跳转至第2个Activity】单击事件代码

3

Intent标准跳转应用

Intent intent = newIntent(FirstActivity.this,

SecondActivity.class);

startActivity(intent);

 SecondActivity.java中【跳转至第1个Activity】单击事件代码

Intent intent = newIntent();

intent.setClass(SecondActivity.this,

FirstActivity.class);

startActivity(intent);

2) Intent带参数Activity跳转(使用Intent.putExtra和Bundle)

窗体之间的跳转也需要参数的传递,我们在Android中实现窗体之间的参数传递主要有两种方式:一种是采用putExtra()方法;另一种是采用Bundle对象进行参数封装传递。

第一种:使用putExtra()方法实现参数传递

➢ 方法语法:putExtra(String name, Object value);

➢ 参数说明:name:代表参数的变量名称, value:代表参数的值

4

Intent标准跳转应用

该方法为Intent对象中的一个方法,使用intent.putExtra(……)来实现参数的设置,那另一端接受方需要获取发送方传入的Intent对象,同时从Intent对象中通过设置的参数名称获取相应的参数值并进行处理,在这里要注意的是除字符串类型的参数外,其余类型的参数获取的时候都需要有默认值,以备无法正常获取参数时,使用默认值替代。

➢ 应用案例

实现两个窗体之间的跳转并使用Intent对象的putExtra方法完成参数

的传递。设置参数后,任然使用startActivity(intent)方法进行页面之间的跳转动作。

➢ 实现参考图

➢ 局部关键实现代码

 SendActivity.java中【发送消息】单击事件动作处理代码

Intent intent = newIntent(SendActivity.this,

5

Intent标准跳转应用

ReceiveActivity.class);

String name = txtName.getText().toString();

int age = Integer.parseInt(txtAge.getText().toString());

intent.putExtra(\"name\", name);

intent.putExtra(\"age\", age);

startActivity(intent);

 ReceiveActivity.java中获取Intent携带参数并显示的处理代码

Intent intent = this.getIntent();

String name = intent.getStringExtra(\"name\");

int age = intent.getIntExtra(\"age\", 0);

this.txtMessage.setText(\"姓名:\"+name+\"\\n年龄:\"+age);

第二种:使用Bundle对象实现参数封装后页面跳转传值

➢ 构造方法:Bundle bundle = new Bundle();

6

Intent标准跳转应用

➢ 对象方法:putXXXX(String name, Object value); 设置传递参数,XXXX代表传递参数的类型。

➢ 参数说明:name:代表参数的变量名称, value:代表参数的值

Bundel对象使用putXXXX()方法将待传递的参数封装到Bundle对象中,使用Intent对象的putExtras(Bundle bundle)方法将参数整体打包进行传递,要获取发送方传入的Bundel对象的时,使用getExtras()方法获取Bundel对象,同时使用getXXX(String name)方法获取Bundle中封装的某个具体参数的变量名称。在这里要注意的是除字符串类型的参数外,其余类型的参数获取的时候都需要有默认值,以备无法正常获取参数时,使用默认值替代。

➢ 应用案例

实现一个新用户信息注册并显示注册信息的功能,该功能使用Bundle对象完成多参数的封装传递,并在接收方实现对Bundle封装数据的处理操作。

➢ 实现参考图片

7

Intent标准跳转应用

➢ 局部关键实现代码

 RegisterActivity.java【马上注册】按钮单击事件处理代码

// 步骤1:获取编辑文本框中用户输入的数据

String name = txtName.getText().toString();

String dept = txtDept.getText().toString();

String job = txtJob.getText().toString();

String email = txtEmail.getText().toString();

// 步骤2:创建Bundled对象并将数据进行封装

Bundle bundle = newBundle();

8

Intent标准跳转应用

bundle.putString(\"name\", name);

bundle.putString(\"dept\", dept);

bundle.putString(\"job\", job);

bundle.putString(\"email\", email);

// 步骤3:创建Intent对象

Intent intent = newIntent();

intent.setClass(RegisterActivity.this,

ResultActivity.class);

// 使用putExtras()方法将Bundle对象装载到Intent中

intent.putExtras(bundle);

startActivity(intent);

 ResultActivity.java 接受Bundle封装的数据并进行获取显示

// 步骤1:获取Intent对象

9

Intent标准跳转应用

Intent intent = this.getIntent();

// 步骤2:使用getExtras()方法获取Bundle对象

Bundle bundle = intent.getExtras();

// 步骤3:使用getXXX(String name)方法获取Bundle中的对应数据

String name = bundle.getString(\"name\");

String dept = bundle.getString(\"dept\");

String job = bundle.getString(\"job\");

String email = bundle.getString(\"email\");

this.txtMessage.setText(\"姓名:\" + name + \"\\n部门:\" +dept

+ \"\\n职位:\" + job+ \"\\n电子邮箱:\" + email);

作者:中软卓越天津ETC

10

因篇幅问题不能全部显示,请点此查看更多更全内容