Activity经典实例一:两个Activity传递数据和对象

房东的猫 提交于 2019-12-03 12:34:01

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;
	}
}


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!