人生没有真正的绝望。树,在秋天放下了落叶,心很疼,可是整个冬天,它让心在平静中积蓄力量。春天一到,芳华依然。一时的成败得失对于一生来说,不过来了一场小感冒。心若累了,让它休息,灵魂的修复是人生永不干枯的希望。
在Activity之间进行数据交换使用了一个”信使”,叫做 Intent.因此我们主要是将需要交换的数据放入 Intent中,即可.
Intent 提供了多个重载的方法,来”携带”额外的数据. 如下所示;
- putExtras(Bundle data); 向Intent中放入需要携带的数据包.
- Bundle getExtras(); 取出所”携带”的数据包.
- putExtra(String name,Xxx values); 向Intent中按key-value 对的形式存入数据.
- getXxxExtra(String name);从 Intent中按key取出指定类型的数据.
上面的方法中,Bundle就是一个简单数据携带包,该Bundle对象包含了多个方法来存入数据. - putXxx(String key,Xxx data);向 Bundle中存入 Int, Long,等各种类型的数据.
- putSerializable(String key,Serializable data); 向 Bundle中放入一个可序列化的对象.
为了取出 Bundle中的数据, Bundle中提供了如下方法;
- getXxx(String key); 从 BUndle中取出 Int, Long,等各种类型的数据.
- getSerializable(String key,Serializable data); 从 Bundle中放入一个可序列化的对象.
思路 写一个BundleActivity 填充数据,然后再写另一个 Activity获取显示数据, 中间通过一个 person 类实现 Java.io.Serializable接口.
填充数据的 Activity
package com.test.bundledemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
/**
* Activity之间的数据传递 利用 Bundle
*/
public class BundleActivity extends Activity {
Button btnRegister;
EditText et_name, et_pwd;
RadioButton male, female;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bundle);
btnRegister = (Button) findViewById(R.id.btn_register);
et_name = (EditText) findViewById(R.id.et_name);
et_pwd = (EditText) findViewById(R.id.et_pwd);
male = (RadioButton) findViewById(R.id.male);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String gender = male.isChecked() ? "男" : "女";
Person p = new Person(et_name.getText().toString(),et_pwd.getText().toString(),gender);
//创建一个 Bundle
Bundle data = new Bundle();
// data.putBundle("person",p);
data.putSerializable("person",p);
Intent intent = new Intent(BundleActivity.this,ReceiveDataActivity.class);
intent.putExtras(data);
startActivity(intent);
}
});
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="vertical"
tools:context="com.test.bundledemo.BundleActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入您的注册信息: " />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名: " />
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您的注册账号"
android:selectAllOnFocus="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码: " />
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您的密码" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性 别:" />
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true"
android:textSize="20dp" />
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
android:textSize="20dp" />
</RadioGroup>
</LinearLayout>
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册" />
</LinearLayout>
接收数据的 Activity
package com.test.bundledemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class ReceiveDataActivity extends AppCompatActivity {
TextView name,pwd,gender;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive_data);
name = (TextView) findViewById(R.id.name);
pwd = (TextView) findViewById(R.id.passwd);
gender = (TextView) findViewById(R.id.gender);
//获取启动该 Aivity的 Intent
Intent intent = getIntent();
//直接通过Intent取出它携带的Bundle 数据包中的数据
Person person = (Person) intent.getSerializableExtra("person");
name.setText("用户名: "+person.getName());
pwd.setText("用户名: "+person.getPwd());
gender.setText("用户名: "+person.getGender());
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.test.bundledemo.ReceiveDataActivity">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp" />
<TextView
android:id="@+id/passwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp" />
<TextView
android:id="@+id/gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp" />
</LinearLayout>
package com.test.bundledemo;
import java.io.Serializable;
/**
* Created by Administrator on 2016/6/10.
*/
public class Person implements Serializable{
private String name;
private String pwd;
private String gender;
public Person() {
}
public Person(String name, String pwd, String gender) {
this.name = name;
this.pwd = pwd;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}