展会信息港展会大全

android开发之两个Activity传递数据和对象
来源:互联网   发布日期:2016-01-14 10:22:38   浏览:1552次  

导读:1、概 述:Activity类直接或者间接地继承了 Context、ContextWrapper、ContextThemeWrapper等基类,因此Activity可以直接调用它们的方法。创建一个Activity需要实现某些方法,常见 的是实现onCreate(Bundle ...

1、概 述:

Activity类直接或者间接地继承了 Context、ContextWrapper、ContextThemeWrapper等基类,因此Activity可以直接调用它们的方法。

创建一个Activity需要实现某些方法,常见 的是实现onCreate(Bundle status)方法,该方法将 会在Activity创建时被回调,它调用setContentView(View view)方法来显示要展示的View。

一个Android应用常常有多个 Activity,但是只有一个作为程序的入口,其他的Activity通常都由入口Activity、及其后者启动。

2、 Activity启动另一个Activity的方法:

startActivity(Intent intent):启动其他Activity;

startActivityForResult(Intent intent, int requestCode):以指定请求码(requestCode)启动Activity, 而且程序将会等到新启动Activity的结果(通过重写onActivityResult(...)方法来获取结果)。

3、关 闭Activity的方法:

finish():结束掉当前的Activity;

finishActivity(int requestCode):结束以startActivityForResult()方法启动的Activity。

4、使 用Bundle在Activity之间交换数据:

1)、Intent: 主要通过Intent这个信使,将需要交换的数据放入即可。Intent提供了方法用于携带数据,如:

putExtras(Bundle data):向Intent中放入需要携带的数据;

2)、Bundle: 就是一个简单的数据包,该Bundle对象包含了多个方法来存入、取出数据,有:

putXxx(String key, Xxx data):向Bundle放入Int、Long等各种类型的数据;

putSerializable(String key, Serializable data):向Bundle放入一个可序列化的对象;

getXxx(String key):从Bundle取出Int、Long等各种类型的数 据;

getSerializable(String key):从Bundle取出一个可序列化的对象。

5、开发实例:注册用户信息

项目简介:程序包含两个Activity,一 个给用户填写信息,另一个显示注册结果。

完整代码:

RegisterActivity.java源代码:

package com.xsjayz.ac;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.RadioButton;

import android.widget.Toast;

public class RegisterActivity extends Activity {

private Button regButton;

private EditText nameEdit;

private EditText passwdEdit;

private RadioButton maleButton;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

regButton = (Button) findViewById(R.id.register_now);

nameEdit = (EditText) findViewById(R.id.name_edit);

passwdEdit = (EditText) findViewById(R.id.password_edit);

maleButton = (RadioButton) findViewById(R.id.male_btn);

regButton.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

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

String passwd = passwdEdit.getText().toString();

String gender = maleButton.isChecked() ? "男" : "女";

// 如果输入信息不完整,则不允许注册。

if (name.equals("") || passwd.equals("")) {

Toast.makeText(RegisterActivity.this, "请填写完整的信息!", 3000)

.show();

} else {

// 创建Person对象,一个可序列化的对象。

Person person = new Person(name, passwd, gender);

Bundle bundle = new Bundle();

bundle.putSerializable("person", person);

Intent intent = new Intent(RegisterActivity.this,

ResultActivity.class);

intent.putExtras(bundle);

// 启动另一个Activity。

startActivity(intent);

}

}

});

}

}

ResultActivity.java 源代码:

package com.xsjayz.ac;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.widget.TextView;

public class ResultActivity extends Activity {

private TextView nameText;

private TextView passwdText;

private TextView genderText;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.result);

nameText = (TextView) findViewById(R.id.name_text);

passwdText = (TextView) findViewById(R.id.passwd_text);

genderText = (TextView) findViewById(R.id.gender_text);

// 获取启动该ResultActivity的Intent

Intent intent = getIntent();

// 获取该Intent所携带的数据

Bundle bundle = intent.getExtras();

// 从bundle数据包中取出数据

Person person = (Person) bundle.getSerializable("person");

// 显示注册结果

nameText.setText("您的帐号:" + person.getName());

passwdText.setText("您的密码:" + person.getPasswd());

genderText.setText("您的性别:" + person.getGender());

}

}

main.xml布局文件:

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

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

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/register_info_text"

android:textSize="20sp" />

<TableRow>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/user_name"

android:textSize="16sp" />

<EditText

android:id="@+id/name_edit"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="@string/set_name"

android:selectAllOnFocus="true" />

</TableRow>

<TableRow>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/user_password"

android:textSize="16sp" />

<EditText

android:id="@+id/password_edit"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="@string/set_password"

android:password="true"

android:selectAllOnFocus="true" />

</TableRow>

<TableRow>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/user_sex"

android:textSize="16sp" />

<RadioGroup

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<RadioButton

android:id="@+id/male_btn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/male_btn"

android:checked="true"

android:textSize="16sp" />

<RadioButton

android:id="@+id/female_btn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/female_btn"

android:textSize="16sp" />

</RadioGroup>

</TableRow>

<Button

android:id="@+id/register_now"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/register_btn"

android:textSize="16sp" />

</TableLayout>

result.xml布局文件:

<?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:id="@+id/name_text"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="18sp" />

<TextView

android:id="@+id/passwd_text"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="18sp" />

<TextView

android:id="@+id/gender_text"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="18sp" />

</LinearLayout>

可序列化的对象Person:

package com.xsjayz.ac;

import java.io.Serializable;

public class Person implements Serializable {

private static final long serialVersionUID = 1L;

private String name;

private String passwd;

private String gender;

public Person(String name, String passwd, String gender) {

this.name = name;

this.passwd = passwd;

this.gender = gender;

}

public String getName() {

return name;

}

public String getPasswd() {

return passwd;

}

public String getGender() {

return gender;

}

}

赞助本站

人工智能实验室

相关热词: 传递数据 Activity android

AiLab云推荐
展开

热门栏目HotCates

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