一、概述: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; } }