Activity經典實例一:兩個Activity傳遞數據和對象

一、概述:java

Activity類直接或者間接地繼承了Context、ContextWrapper、ContextThemeWrapper等基類,所以Activity能夠直接調用它們的方法。android

建立一個Activity須要實現某些方法,常見的是實現onCreate(Bundle status)方法,該方法將會在Activity建立時被回調,它調用setContentView(View view)方法來顯示要展現的View。app

一個Android應用經常有多個Activity,可是隻有一個做爲程序的入口,其餘的Activity一般都由入口Activity、及其後者啓動。ide


二、Activity啓動另外一個Activity的方法:佈局

startActivity(Intent intent):啓動其餘Activity;this

startActivityForResult(Intent intent, int requestCode):以指定請求碼(requestCode)啓動Activity,並且程序將會等到新啓動Activity的結果(經過重寫onActivityResult(...)方法來獲取結果)。spa


三、關閉Activity的方法:code

 finish():結束掉當前的Activity;xml

 finishActivity(int requestCode):結束以startActivityForResult()方法啓動的Activity。對象


四、使用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取出一個可序列化的對象。


五、開發實例:註冊用戶信息

項目簡介:程序包含兩個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;
	}
}
相關文章
相關標籤/搜索