昨天发表了第一篇博客,虽然不像大牛们那么厉害,但对于入门的小伙伴们来说确实很实用,昨天好多小伙伴给我说intent传递数据少了一种使用bundle的,其实本来是想写在那一篇的,后来我觉得应该单独拿出来,这样就不会混淆。废话不多说,来,跟随我的脚步走进bundle滴内心世界。
一、Bundle传递简单数据类型
因为Bundle中已经封装好了简单数据类型,比如:String,int等,那我们该如何通过Bundle来传递数据呢,跟上我的脚步,去探索Bundle的简单数据传递。代码如下:
在MainActivity中:
// 实例化一个Bundle
Bundle bundle = new Bundle();
// 实例化一个intent
Intent intent = new Intent(MainActivity.this, ShowActivity.class);
// 获取输入的数据
String name = mName.getText().toString();
String num = mNumber.getText().toString();
// 把数据保存到Bundle里
bundle.putString("name", name);