Activity間のデータインタラクション

8045 ワード

Activity間のデータインタラクション、4つの主な方法:
 * 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);
		}
	}
	
}