第二十三讲:Android之Bundle类

九牛一毫莫自夸,骄傲自满必翻车。历览古今多少事,成由谦逊败由奢。—— 陈  毅


本讲内容:Bundle类

一、什么是Bundle:Bundle是携带数据,它类似于Map(集合类),用于存放key-value名值对形式的值,可以实现两个activity之间的通讯


类继承关系:

java.lang.Object
     android.os.Bundle

二、 Intent传值和Bundle传值是一回事:下面可以看出Intent传值其实它内部还是调用了Bundle

public Intent putExtra(String name, boolean value) { 
if (mExtras == null) { 
mExtras = new Bundle(); 
} 
mExtras.putBoolean(name, value); 
return this; 
}

三、 我们通过一个例子感受一下,代码的讲解都写在注释里了

下面是res/layout/activity_main.xml 布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.text.MainActivity$PlaceholderFragment" >
 
  <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Bundle类的用法"
        android:textSize="30sp"
        android:gravity="center" />
    <Button 
        android:id="@+id/b1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击跳转"
        android:textSize="25sp"/>
    
</LinearLayout>
下面是新建res/layout/activity_second.xml 布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView 
        android:id="@+id/textViewId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我是第二个Activity"/>
    
</LinearLayout>

下面是MainActivity.java主界面文件:

public class MainActivity extends Activity implements OnClickListener{
	private Button b;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		b=(Button) findViewById(R.id.b1);
		b.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		Intent intent=new Intent(MainActivity.this,SecondActivity.class);
		Bundle bundle=new Bundle();
		bundle.putString("1", "data from TestBundle");//加入数据  
		intent.putExtras(bundle);//将该bundle加入这个intent对象
		startActivity(intent);
	}
}

注意:bundle类中加入数据(key -value的形式,另一个activity里面取数据的时候,就要用到key,找出对应的value)



建一个新的项目SecondActivity.java 界面文件:

public class SecondActivity extends Activity{
	private TextView t1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		
		t1=(TextView) findViewById(R.id.textViewId);
		//得到传过来的bundle  
		Bundle bundle=getIntent().getExtras();
		String data=bundle.getString("1");取出数据 
		setTitle(data);
	}
}

getIntent得到一个Intent,是指上一个activity启动的intent,这个方法返回intent对象,然后调用intent.getExtras()得到intent所附带的额外数据

新建一个java项目要在AndroidManifest.xml中注册如下:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.text.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity"></activity>
    </application>

下面是运行结果:


点击后



本讲到这里,谢谢大家!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值