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.shhuangpu.userlogin;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
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;
import android.view.Gravity;
public class LoginMainActivity extends Activity {
private Button regButton;
private EditText nameEdit;
private EditText passwdEdit;
private RadioButton maleButton;
private Toast myToast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_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(LoginMainActivity.this,
"请填写完整的信息!", 3000).show();
//
myToast=Toast.makeText(getApplicationContext(),getText(R.string.mybutton2_text)+myeditText1.getText().toString(),Toast.LENGTH_LONG);
//
myToast.setGravity(Gravity.CENTER, 0, 0);
//
myToast.show();
myToast =
Toast.makeText(LoginMainActivity.this, "请填写完整的信息!",Toast.LENGTH_LONG);
myToast.setGravity(Gravity.CENTER,0,0);
myToast.show();
}
else {
// 创建Person对象,一个可序列化的对象。
Person person = new Person(name, passwd, gender);
Bundle bundle = new Bundle();
bundle.putSerializable("person", person);
Intent intent = new Intent(LoginMainActivity.this,ResultActivity.class);
intent.putExtras(bundle);
// 启动另一个Activity。
startActivity(intent);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action
bar if it is present.
getMenuInflater().inflate(R.menu.activity_login_main, menu);
return true;
}
}
ResultActivity.java源代码:
package com.shhuangpu.userlogin;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.widget.TextView;
public class ResultActivity extends Activity {
private TextView nameText;
private TextView passwdText;
private TextView genderText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action
bar if it is present.
getMenuInflater().inflate(R.menu.activity_result, menu);
return true;
}
}
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="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/set_name"
android:inputType="textPersonName"
android:visibility="visible" >
<requestFocus />
EditText>
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="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/set_password"
android:inputType="textPassword" />
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.shhuangpu.userlogin;
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;
}
}
strings.xml
?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">UserLoginstring>
<string name="menu_settings">Settingsstring>
<string name="title_activity_result">ResultActivitystring>
<string name="register_info_text">请输入您的注册信息string>
<string name="user_name">帐号:string>
<string name="set_name">输入帐号string>
<string name="user_password">密码:string>
<string name="set_password">输入密码string>
<string name="user_sex">性别:string>
<string name="male_btn">男string>
<string name="female_btn">女string>
<string name="register_btn">注册string>
resources>