使用Intent Bundle在Activity间传递数据
来源:互联网 发布:单片机显示大字代码 编辑:程序博客网 时间:2024/05/24 07:03
前面的帖子介绍过Bundle类:http://blog.csdn.net/zhangjikuan/article/details/20528653
这里说一下Intent Bundle在Activity间传递数据
activity间传递数据的方法又两种
1.直接通过intent:(intent本来就是activity间传递信息的eg:一个activity启动另一个activity)
intent对象的方法:putExtra(string name ,Xxx value)//Xxx可为int long string array
getXxxExtra(string name )
注:此方法是智能的,当intent携带bundle的时候调用此方法会直接把数据添加到bundle中
2.通过intent和bundle:
intent对象的方法putExtas( Bundle data) Bundle getExtras()//返回的是bundle数据
bundle对象的方法 putXxx(string key,Xxx data); putSerializable(string key ,Serializable data)//放入一个可序列化的对象
getXxx(string key) getSerializable(string key ,Serializable)
下面的例子是用一个activity显示并可输入注册信息,点击注册会将数据传送个另一个activity显示出来注册信息
/**
* 此文件是显示注册信息,并将注册信息通过Intent和bundle发送给ResultActivity
*/
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bn = (Button) findViewById(R.id.bn);
bn.setOnClickListener(new OnClickListener()
{
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 //person是自己定义的一个可序列化的接口
.getText().toString(), gender);
// 创建一个Bundle对象
Bundle data = new Bundle();
data.putSerializable("person", p);
// 创建一个Intent
Intent intent = new Intent(MainActivity.this,
ResultActivity.class);
intent.putExtras(data);
// 启动intent对应的Activity
startActivity(intent);
}
});
}
}
**
* 此activity文件是接收MainActivity文件传送过来的数据并显示
*/
public class ResultActivity extends Activity
{
@Override
public 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);
// 获取启动该Result的Intent
Intent intent = getIntent();
// 直接通过Intent取出它所携带的Bundle数据包中的数据
Person p = (Person) intent.getSerializableExtra("person");
name.setText("您的用户名为:" + p.getName());
passwd.setText("您的密码为:" + p.getPass());
gender.setText("您的性别为:" + p.getGender());
}
}
/**
*这是定义了一个可序列化的接口
*/
public class Person implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String pass;
private String gender;
public Person()
{
}
/**
* @param name
* @param pass
* @param gender
*/
public Person(String name, String pass, String gender)
{
this.name = name;
this.pass = pass;
this.gender = gender;
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getPass()
{
return pass;
}
public void setPass(String pass)
{
this.pass = pass;
}
public String getGender()
{
return gender;
}
public void setGender(String gender)
{
this.gender = gender;
}
}
最后注意在AndroidManifest文件中的配置
- 使用Intent Bundle在Activity间传递数据
- 使用Bundle在Activity间传递数据
- 使用Bundle在Activity间传递数据
- 使用 Bundle在Activity间传递数据
- 使用Bundle在Activity间传递数据
- 使用bundle在activity间传递数据
- Android Intent.bundle 在activity中一对一传递数据
- 使用Bundle在Activity之间传递数据
- 使用Bundle在Activity之间传递数据
- 使用bundle在Activity之间传递数据
- Android 使用Bundle在Activity间传递数据
- Activity中Intent传递数据--Bundle
- 使用Intent在activity之间传递数据
- andriod开发点滴(使用Bundle,Intent在Activity间传递map)
- 使用Bundle在Activity间传递信息
- 在安卓中使用Bundle在Activity之间传递数据
- 用intent在activity间传递数据
- 用Intent在Activity间传递数据
- Android Binder机制浅析
- No exports were found that match the constraint.
- 第64条:for-each循环优于for循环
- 软件下载地址
- MySQL的数据库引擎的类型
- 使用Intent Bundle在Activity间传递数据
- phpredis 可以用来存储php的session
- 使用 Docker/LXC 迅速启动一个桌面系统
- java冒泡排序
- string的笔试!
- 机房收费系统——UML图
- 手动创建ORACLE数据库
- linux下搭建ipv6的pppoe server端
- java选择排序