Android学习笔记<三>

本文通过示例展示了如何在Android应用中使用Intent进行数据传递,从MainActivity到NextActivity,包括使用Bundle封装复杂数据类型,并在NextActivity中提取并使用这些数据。

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

Activity 值的传递:


1、(MainActivity.java代码:意图传递值)

Intent intent = new Intent(MainActivity.this, NextActivity.class);
intent.putExtra("name", "jack");
intent.putExtra("age", 23);
intent.putExtra("address", "北京");

Bundle bundle = new Bundle();
bundle.putString("code", "1001");

intent.putExtra("bundle", bundle);
StartActivity(intent);

(NextActivity.java代码:提取数据)

<pre name="code" class="html">Intent intent = getIntent(); // getIntent()是将项目中包含的原始intent检索出来;整句是将检索出来的intent赋值给一个Intent类型的变量intent  
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 0);
String address = intent.getStringExtra("address");

Bundle bundle =intent.getBundleExtra("bundle");
String code = bundle.getString("code");


2、 调试程序

<pre name="code" class="html">...
private final TAG = "NextActivity";
...
Intent intent = getIntent(); // getIntent()是将项目中包含的原始intent检索出来;整句是将检索出来的intent赋值给一个Intent类型的变量intent  
String name = intent.getStringExtra("name");
Log.i(TAG, "-->>" + name); // 前面表示要过滤的目标标签
int age = intent.getIntExtra("age", 0);
Log.i(TAG, "-->>" + age); 
String address = intent.getStringExtra("address");
Log.i(TAG, "-->>" + address); 
Bundle bundle =intent.getBundleExtra("bundle");
String code = bundle.getString("code");
Log.i(TAG, "-->>" + code); 


在LogCat(deprecated)的Filter框输入"--"进行过滤查看。

点击"+"(CreateFilter),

       filter name : NextActivity

       by log tag: NextActivity

       by log level: Info

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值