Activity之间参数传递

本文详细介绍了在Android开发中如何进行Activity之间的参数传递,包括使用Intent传递基本类型数据、Parcelable接口序列化复杂对象、Bundle保存数据以及启动模式的影响。通过对各种方式的对比和实践,理解在不同场景下选择合适的数据传递方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、第一种,简单传递。

发送:

switch (v.getId()) {

case R.id.btn1:

EditText editText = findViewById(R.id.editText);

Intent intent = new Intent(this, my1Activity.class);

intent.putExtra("data", editText.getText().toString());

startActivity(intent);

break;

接收:

Intent intent=getIntent();

String str1 = intent.getStringExtra("data");

TextView textView=findViewById(R.id.textView3);

textView.setText(str1);

2、第二种:使用bundle.外汇常见问题
http://www.kaifx.cn/lists/question/

发送:

Bundle bundle=new Bundle();

bundle.putString("data1",editText.getText().toString()+editText.getText().toString());

intent.putExtras(bundle);

startActivity(intent);

接收:

Bundle bundle=getIntent().getExtras();

textView1.setText(bundle.getString("data1").toString());

3、相对复杂:

发送端:

case R.id.btn1:

EditText editText = findViewById(R.id.editText);

Intent intent = new Intent(this, my1Activity.class);

//定义。

Mapmap=new HashMap<>();

map.put("key1","Value1");

map.put("key2","Value1");

List> list=new ArrayList<>();

list.add(map);

//须定义一个list用于在budnle中传递需要传递的

ArrayList<Object>,这个是必须要的!
Bundle bundle=new Bundle();
ArrayList bundlelist=new ArrayList();
bundlelist.add(list);
bundle.putParcelableArrayList("list",bundlelist);
intent.putExtras(bundle);
startActivity(intent);
break;

接收:

//接收参数
Bundle bundle=getIntent().getExtras();
ArrayList list =bundle.getIntegerArrayList("list");
//把list中的参数,转换会
List<Map<String,Object>> lists=( List<Map<String,Object>>)list.get(0);

String sResult = "";
for (Map<String, Object> m : lists) {
for (String k : m.keySet()) {
sResult += "\r\n" + k + " : " + m.get(k);
}
}

TextView textView=findViewById(R.id.textView3);
TextView textView1=findViewById(R.id.textView4);
textView1.setText(sResult);

4、定义全局变量:
定义:

public class my_data{
public static String m1;
public static Integer d1;
}
使用:


textView.setText(my_data.m1);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值