标签:
1 package com.gaojinhua.android.activitymsg; 2 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.support.v7.app.AppCompatActivity; 6 import android.view.View; 7 import android.widget.EditText; 8 import android.widget.TextView; 9 10 public class MainActivity extends AppCompatActivity { 11 private EditText etName; 12 private EditText etAge; 13 private TextView textView; 14 public static final String DATA = "data"; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 findViewById(R.id.btnStartAty).setOnClickListener(new View.OnClickListener() { 21 @Override 22 public void onClick(View v) { 23 etName = (EditText) findViewById(R.id.etName); 24 etAge = (EditText) findViewById(R.id.etAge); 25 textView = (TextView) findViewById(R.id.textView); 26 User user = new User(etName.getText().toString(), Integer.parseInt(etAge.getText().toString())); 27 Bundle b = new Bundle(); 28 b.putParcelable(DATA, user); 29 Intent i = new Intent(MainActivity.this, Aty.class); 30 i.putExtras(b); 31 startActivityForResult(i, 0); 32 } 33 }); 34 } 35 36 @Override 37 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 38 super.onActivityResult(requestCode, resultCode, data); 39 textView.setText("另一个Activity返回的数据是:" + data.getExtras().getString(DATA)); 40 } 41 42 43 }
1 package com.gaojinhua.android.activitymsg; 2 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.support.v7.app.AppCompatActivity; 6 import android.view.View; 7 import android.widget.TextView; 8 9 public class Aty extends AppCompatActivity { 10 private TextView textViewAty; 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_aty); 16 Intent i = getIntent(); 17 textViewAty = (TextView) findViewById(R.id.textViewAty); 18 Bundle b = i.getExtras(); 19 User user = b.getParcelable(MainActivity.DATA); 20 textViewAty.setText(String.format("User info(name=%s,age=%d)", user.getName(), user.getAge())); 21 22 findViewById(R.id.btnBack).setOnClickListener(new View.OnClickListener() { 23 @Override 24 public void onClick(View v) { 25 Intent i = new Intent(); 26 Bundle b = new Bundle(); 27 b.putString(MainActivity.DATA, textViewAty.getText().toString()); 28 i.putExtras(b); 29 setResult(1, i); 30 finish(); 31 } 32 }); 33 } 34 35 }
1 package com.gaojinhua.android.activitymsg; 2 3 import android.os.Parcel; 4 import android.os.Parcelable; 5 6 /** 7 * Created by Gaojinhua on 2015/8/14. 8 */ 9 public class User implements Parcelable { 10 private String name; 11 private int age; 12 13 public User(String name, int age) { 14 this.name = name; 15 this.age = age; 16 } 17 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 public String getName() { 24 return name; 25 } 26 27 public void setAge(int age) { 28 this.age = age; 29 } 30 31 public int getAge() { 32 return age; 33 } 34 35 @Override 36 public int describeContents() { 37 return 0; 38 } 39 40 @Override 41 public void writeToParcel(Parcel dest, int flags) { 42 dest.writeString(getName()); 43 dest.writeInt(getAge()); 44 } 45 46 public static final Creator<User> CREATOR = new Creator<User>() { 47 @Override 48 public User createFromParcel(Parcel source) { 49 return new User(source.readString(), source.readInt()); 50 } 51 52 @Override 53 public User[] newArray(int size) { 54 return new User[size]; 55 } 56 }; 57 58 59 }
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context=".MainActivityFragment"> 11 12 <EditText 13 android:id="@+id/etName" 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 android:hint="请输入您的名字" 17 android:singleLine="true" 18 android:text="" /> 19 20 <EditText 21 android:id="@+id/etAge" 22 android:layout_width="fill_parent" 23 android:layout_height="wrap_content" 24 android:hint="请输入你的年龄" 25 android:singleLine="true" 26 android:text="" /> 27 28 <Button 29 android:id="@+id/btnStartAty" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:layout_alignParentLeft="true" 33 android:layout_alignParentStart="true" 34 android:layout_below="@+id/et" 35 android:layout_marginTop="126dp" 36 android:text="下一页" /> 37 38 <TextView 39 android:id="@+id/textView" 40 android:layout_width="wrap_content" 41 android:layout_height="wrap_content" /> 42 43 44 </LinearLayout>
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.gaojinhua.android.activitymsg.AtyFragment"> 11 12 <TextView 13 android:id="@+id/textViewAty" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:text="" /> 17 18 <Button 19 android:id="@+id/btnBack" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:text="回到上一页" /> 23 24 25 </LinearLayout>
标签:
原文地址:http://www.cnblogs.com/gaojinhua/p/4731466.html