您的位置:首页 > 其它

使用Bundle在Activity间传递信息

2016-08-21 11:02 381 查看
 输入代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入您的注册信息:"
android:textSize="20sp"/>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="16sp"/>
<!--定义一个EditText,用于收集用户的账号-->
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入想注册的账号"
android:selectAllOnFocus="true"
android:layout_column="1" />
</TableRow>

<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="密    码:"
android:textSize="16sp"/>

<!--用于收集用户的密码-->
<EditText
android:id="@+id/passwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:password="true"
android:selectAllOnFocus="true"
android:layout_column="1" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="性    别 :"
android:textSize="16sp"
android:layout_column="0" />

<!--定义一组单选按钮,用于收集用户注册的性别-->
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/male"
android:text="男"
android:textSize="16sp"
android:layout_column="1" />

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/female"
android:text="女"
android:textSize="16sp" />
</TableRow>

<Button
android:id="@+id/bn"
android:text="注  册"
android:textSize="16sp"
android:layout_column="0" />
</TableLayout>


MainActivity.java

package com.example.haige.zhuceactivity;

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;

public class MainActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bn=(Button)findViewById(R.id.bn);
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText name=(EditText)findViewById(R.id.name);
EditText passwd=(EditText)findViewById(R.id.passwd);
RadioButton male=(RadioButton)findViewById(R.id.male);
String gender=male.isChecked()?"男":"女";
Person p=new Person(name.getText().toString(),passwd.getText().toString(),gender);
//创建一个Bundle对象
Bundle data=new Bundle();
data.putSerializable("person",p);
//创建一个Intent
Intent intent=new Intent(MainActivity.this,ResultActivity.class);
intent.putExtras(data);
startActivity(intent);
}
});
}
}


第一的Activity显示的窗口



result.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<!--定义三个TextView,用于显示用户输入的数据-->

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:id="@+id/name" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:id="@+id/passwd" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:id="@+id/gender" />
</LinearLayout>



ResultActivity.java

package com.example.haige.zhuceactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import java.io.Serializable;

public class ResultActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
TextView name=(TextView)findViewById(R.id.name);
TextView passwd=(TextView)findViewById(R.id.passwd);
TextView gender=(TextView)findViewById(R.id.gender);
//获取启动该Activity的Intent
Intent intent=getIntent();
//直接通过Intent取出它所携带的Bundle数据包中的数据
Person p=(Person) intent.getSerializableExtra("person");
name.setText("您的用户名:"+p.getName());
passwd.setText("您的密码为:"+p.getPasswd());
gender.setText("您的性别为:"+p.getGender());
}
}


 注册成功后显示的信息:



        下面以使用getSerializable为例,定义一个可序列化的Person类,模拟一个用户注册的过程,通过注册那个窗口(Acitivity)传递注册信息到另一个窗口,下面是定义的一个DTO类Person用来记录注册的信息,注意!要定义成可序列化的类,继承Serializable。

Person.java

package com.example.haige.zhuceactivity;

import java.io.Serializable;

public class Person implements Serializable
{
private String _Name;
private String _Passwd;
private String _Gender;
public String getName()
{
return _Name;
}
public String getPasswd()
{
return _Passwd;
}
public String getGender()
{
return _Gender;
}
public Person(String Name,String Passwd,String Gender)
{
this._Name = Name;
this._Passwd = Passwd;
this._Gender = Gender;
}
}


 总结:

     1.在Android中,两个Acitivity之间是靠Intent传递信息的,因为Intent本来就起到信使的作用,所以用它来传递数据也显得顺理成章了,Intent 提供了多个方法来"携带"额外的数据。

     2.用到的知识点:

putExtras(Bundle data): 向Intent中放入需要"携带"的数据

putXxx(String key,Xxx date):向Bundle放入Int,Long等各种类型的数据(Xxx指代各种数据类型的名称)

putSerializable(String key,Serializable date):向Bundle中放入一个可序列化的对象.

当然Intent也提供了相应的取出"携带"数据的方法

getXxx(String key):从Bundle取出Int,Long 等各种数据类型的数据.

getSerializable(String Key,Serializable data): 从Bundle取出一个可序列化的对象.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: