处理注册信息的小例子(bundle传递数据)
2015-08-30 11:06
316 查看
1.所包含的Activity共两个MainActivity,ResultActivity,一个Person类
a.创建person对象,用来存储数据注册信息;
b.创建bundle对象,用来存储person数据;
c.将person数据闯入bundle中并命名为person;
d.创建一个intent用作页面跳转;
e.将bundle传入intent中;
f.启动intent;
g.在ResultActivity中获取数据即可;
注意:ResultActivity不能忘了在Mainfest中注册;
package com.cbg.bundle; 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; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button bn = (Button) findViewById(R.id.bn); bn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub EditText name = (EditText) findViewById(R.id.name); EditText pssword = (EditText) findViewById(R.id.password); RadioButton male = (RadioButton) findViewById(R.id.male); String gender = male.isChecked() ? "男" : "女"; Person p = new Person(name.getText().toString(), pssword .getText().toString(), gender); Bundle data = new Bundle(); data.putSerializable("person", p); Intent intent = new Intent(MainActivity.this, ResultActivity.class); intent.putExtras(data); startActivity(intent); } }); } }
package com.cbg.bundle; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class ResultActivity extends Activity { TextView name, password, gender; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.result); name = (TextView) findViewById(R.id.name); password = (TextView) findViewById(R.id.passwordo); gender = (TextView) findViewById(R.id.gender); // 获取启动该result的Internet Intent intent = getIntent(); // 直接通过Intent去除他所携带的Bundle数据包中的数据 Person person = (Person) intent.getSerializableExtra("person"); name.setText("您的用户名为:" + person.getName()); password.setText("您的用户名为:" + person.getPassword()); gender.setText("您的用户名为:" + person.getGender()); } }
package com.cbg.bundle; import java.io.Serializable; public class Person implements Serializable { /** * */ private static final long serialVersionUID = 1L; /** * */ String name; String password; String gender; public Person(String name, String password, String gender) { super(); this.name = name; this.password = password; this.gender = gender; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } }2,
a.创建person对象,用来存储数据注册信息;
b.创建bundle对象,用来存储person数据;
c.将person数据闯入bundle中并命名为person;
d.创建一个intent用作页面跳转;
e.将bundle传入intent中;
f.启动intent;
g.在ResultActivity中获取数据即可;
注意:ResultActivity不能忘了在Mainfest中注册;
相关文章推荐
- 用位运算实现四则运算之加减乘除
- 三分钟理解“外观模式”——设计模式轻松掌握
- Bootstrap Modals(模态框)
- ListView 用simpleadapter初始化数据
- linux下修改path
- Android开发——Handler引起的内存泄露
- Codeforces Round #318 574D Bear and Blocks(dp)
- 返乡见闻
- <LeetCode(Java版)>Two Sum
- 第一个异常ssh中lib导包重复
- QML入门教程
- JavaSE---API之集合(Map及其子类、Collections)
- NSDateFormatter美国时间格式转换
- 一些设计上的基本常识 - 梁飞
- [LeetCode#127]Word Ladder
- c语言构造数据类型
- win32程序调试时遇到set一闪而过解决
- java基础的面试题(一)
- 简单之美-Dubbo架构设计详解
- HP实训