Activity之间的数据交互

 Activity之间的数据交互,四种主要方式:

 * 1、intent.putExtra("name", "王三");

 * 2、bundle.putString("name", "李四"); intent.putExtras(bundle);
 * 3、bundle2.putSerializable("person", person); intent.putExtras(bundle2);

 * 4、bundle3.putParcelable("img", bitmap); intent.putExtras(bundle3);

public class ActivityTransValue_A extends Activity implements OnClickListener{

	private Button btn_transvalue1, btn_transvalue2, btn_transvalue3, btn_transvalue4;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_transvalue_a);
		
		btn_transvalue1 = (Button) findViewById(R.id.btn_transvalue1);
		btn_transvalue2 = (Button) findViewById(R.id.btn_transvalue2);
		btn_transvalue3 = (Button) findViewById(R.id.btn_transvalue3);
		btn_transvalue4 = (Button) findViewById(R.id.btn_transvalue4);
		btn_transvalue1.setOnClickListener(this);
		btn_transvalue2.setOnClickListener(this);
		btn_transvalue3.setOnClickListener(this);
		btn_transvalue4.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		switch(v.getId()){
		case R.id.btn_transvalue1:
			intent.setClass(ActivityTransValue_A.this, ActivityTransValue_B.class);
			intent.putExtra("name", "王三");
			intent.putExtra("age", 17);
			startActivity(intent);
			break;
			
		case R.id.btn_transvalue2:
			intent.setClass(ActivityTransValue_A.this, ActivityTransValue_C.class);
			Bundle bundle = new Bundle();
			bundle.putString("name", "李四");
			bundle.putInt("age", 11);
			intent.putExtras(bundle);
			startActivity(intent);
			break;
		case R.id.btn_transvalue3:
			intent.setClass(ActivityTransValue_A.this, ActivityTransValue_D.class);
			ActivityTransValue_Person person = new ActivityTransValue_Person("赵柳",22,"青岛");
			Bundle bundle2 = new Bundle();
			bundle2.putSerializable("person", person);
			intent.putExtras(bundle2);
			startActivity(intent);
			break;
			
		case R.id.btn_transvalue4:
			intent.setClass(ActivityTransValue_A.this, ActivityTransValue_E.class);
			Bundle bundle3 = new Bundle();
			Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
			bundle3.putParcelable("img", bitmap);
			intent.putExtras(bundle3);
			startActivity(intent);
			break;
		}
	}
	
}

activity_transvalue_a.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.imooc.activitystudy.OneActivity" >

    <Button 
        android:id="@+id/btn_transvalue1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="传值方式1" />
    
    <Button 
        android:layout_below="@id/btn_transvalue1"
        android:id="@+id/btn_transvalue2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="传值方式2" />
    
    <Button 
        android:layout_below="@id/btn_transvalue2"
        android:id="@+id/btn_transvalue3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="传值方式3" />
    
    <Button 
        android:layout_below="@id/btn_transvalue3"
        android:id="@+id/btn_transvalue4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="传值方式4" />

</RelativeLayout>

public class ActivityTransValue_B extends Activity{

	private TextView tv_value;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_transvalue_b);
		tv_value = (TextView) findViewById(R.id.tv_value);
		
		Intent intent = getIntent();
		if(intent != null){
			String name = intent.getStringExtra("name");
			int age = intent.getIntExtra("age",0);
			tv_value.setText("intent.putExtra传值:  "+"name:"+name+"——"+"age:"+age);
		}
	}
	
}

activity_transvalue_b.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.imooc.activitystudy.OneActivity" >

    <TextView
        android:id="@+id/tv_value"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <ImageView 
        android:layout_below="@id/tv_value"
        android:id="@+id/im_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

public class ActivityTransValue_C extends Activity{

	private TextView tv_value;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_transvalue_b);
		tv_value = (TextView) findViewById(R.id.tv_value);
		
		Intent intent = getIntent();
		if(intent != null){
			String name = intent.getStringExtra("name");
			int age = intent.getIntExtra("age",0);
			tv_value.setText("intent.putExtras(bundle)传值:  "+"name:"+name+"——"+"age:"+age);
		}
	}
}

public class ActivityTransValue_D extends Activity{

	private TextView tv_value;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_transvalue_b);
		tv_value = (TextView) findViewById(R.id.tv_value);
		
		Intent intent = getIntent();
		if(intent != null){
			ActivityTransValue_Person person = (ActivityTransValue_Person) intent.getSerializableExtra("person");
			tv_value.setText("intent.putExtra传值(putSerializable):  "+person.toString());
		}
	}
	
}

public class ActivityTransValue_Person implements Serializable{

	private String name;
	private int age;
	private String address;
	
	public ActivityTransValue_Person(String name, int age, String address) {
		super();
		this.name = name;
		this.age = age;
		this.address = address;
	}
	
	public String toString(){
		return "[name="+name+";age="+age+";address="+address+"]";
	}
}

public class ActivityTransValue_E extends Activity{

	private TextView tv_value;
	private ImageView im_value;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_transvalue_b);
		tv_value = (TextView) findViewById(R.id.tv_value);
		im_value = (ImageView)findViewById(R.id.im_value);
		
		Intent intent = getIntent();
		if(intent != null){
			Bitmap bitmap = intent.getParcelableExtra("img");
			im_value.setImageBitmap(bitmap);
		}
	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值