使用Intent传递参数
来源:互联网 发布:什么租房软件最好 编辑:程序博客网 时间:2024/06/05 10:36
1.使用PutExtra()传递参数
在mainActivity中设置Intent
Intent i = new Intent(MainActivity.this,AnotherAty.class); i.putExtra("name","john");startActivity(i);
在第二个Activity中设置
Intent i = getIntent();textView.setText(i.getStringExtra("data"));
2.使用Bundle传递参数
- 在mainActivity中设置
Intent i = new Intent(MainActivity.this,AnotherAty.class);Bundle b = new Bundle();b.putString("name","john");b.putInt("age",2);i.putExtras(b);startActivity(i);
在第二个Activity中设置:
Intent i = getIntent();Bundle data = i.getExtras();textView.setText(String.format("name=%s,age=%d,name2=%s",data.getString("name"),data.getInt("age"),data.getString("name1","leo")));//第三个是使用默认的设置,当第三个name1为空是,选择默认的信息传递
- 也是使用Bundle传递参数
在MainActivity中这样设置
Intent i = new Intent(MainActivity.this,AnotherAty.class); Bundle b = new Bundle(); b.putString("name","john"); b.putInt("age",2); i.putExtra("bundle",b); startActivity(i);
在另一个Activity中这样设置
Intent i = getIntent(); Bundle data = i.getBundleExtra("bundle"); textView = (TextView) findViewById(R.id.text_view); textView.setText(String.format("name=%s,age=%d,name2=%s",data.getString("name"),data.getInt("age"),data.getString("name1","leo")));
传递值对象
使用Serializable序列化接口来实现:
先创建一个User类:
public class User implements Serializable { private String name; private int age; public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public int getAge() { return age; } public User(String name,int age){ this.name = name; this.age = age; }}
在MainActivity中写入:
Intent i = new Intent(MainActivity.this,AnotherAty.class); i.putExtra("user", new User("John",20)); startActivity(i);
随后,在另一个Activity中:
Intent i = getIntent(); textView = (TextView) findViewById(R.id.text_view); User user = (User) i.getSerializableExtra("user"); textView.setText(String.format("User info(name = %s,age = %d)",user.getName(),user.getAge()));
0 0
- 使用Intent传递参数
- Intent传递参数URI使用示例
- Android使用Intent传递复杂参数及复杂参数列表
- Android使用Intent传递复杂参数及复杂参数列表
- 通过Intent传递参数
- Activity 参数传递 Intent
- intent传递参数
- intent传递参数
- Android intent传递参数
- Intent传递URL参数
- Android---intent传递参数
- Intent传递参数,接收参数
- android 使用intent传递参数实现乘法计算
- Android,使用Intent或Bundle传递参数,跳转页面。
- Android,使用Intent或Bundle传递参数,跳转页面。
- 使用Intent来启动Activity并传递参数
- Android Bundle的使用及与Intent传递参数区别
- Android Bundle的使用及与Intent传递参数区别
- HDU 5821 Ball (排序、思维题)
- 1006. Sign In and Sign Out (25)
- Android 自定义ScrollView ListView 体验各种纵向滑动的需求
- 写作的重要性(关于理工科生)
- HDOJ 1950 Bridging signals
- 使用Intent传递参数
- 输出1到最大的N位数
- 《STL系列》之vector原理及实现
- 116单向TSP
- Android-屏幕适配
- life will in "if......then"
- 【NOIP2016提高A组8.11】钱仓
- HDU 5826 physics(积分 找规律)——2016 Multi-University Training Contest 8
- 任意进制转换