首页 > 代码库 > Activity之间数据传递(一)(简单传递,数据包Bundle)

Activity之间数据传递(一)(简单传递,数据包Bundle)

一,简单传递(简单的字符串)

第一个activity通过putExtra()将字符串传入i

 1  protected void onCreate(Bundle savedInstanceState) { 2         super.onCreate(savedInstanceState); 3         setContentView(R.layout.activity_main); 4         findViewById(R.id.btnStartAty).setOnClickListener(new View.OnClickListener() { 5             @Override 6             public void onClick(View v) { 7                 Intent i= new Intent(MainActivity.this,TheAty.class); 8                 i.putExtra("date","Hello SWWWWWW"); 9                 startActivity(i);10 11             }12         });13     }

第二个activity通过getIntent()获取之前put进来的数据,然后再set到UI界面中去

  
  private TextView tv;

1
protected void onCreate(Bundle savedInstanceState) {2 super.onCreate(savedInstanceState);3 setContentView(R.layout.activity_the_aty);4 5 Intent i =getIntent();6 7 tv=(TextView) findViewById(R.id.tv);

      //通过“date”关键字进行添加
8 tv.setText(i.getStringExtra("date"));9 }

二,通过 包Bundle进行传递

通过首先将数据传入Bundle包,然后将bundle包放入第二个activity

然后再通过第二个activity获取这个包

 1  protected void onCreate(Bundle savedInstanceState) { 2         super.onCreate(savedInstanceState); 3         setContentView(R.layout.activity_main); 4         findViewById(R.id.btnStartAty).setOnClickListener(new View.OnClickListener() { 5             @Override 6             public void onClick(View v) { 7                 Intent i= new Intent(MainActivity.this,TheAty.class); 8                 Bundle b=new Bundle(); 9                 b.putString("name","SWWWWW");10                 b.putInt("age",21);11                 b.putString("depart","KuaiJi");12                 i.putExtras(b);13                 startActivity(i);14 15             }16         });17     }

第二个activity获取包时:

1
private TextView tv;
 
protected void onCreate(Bundle savedInstanceState) {2 super.onCreate(savedInstanceState);3 setContentView(R.layout.activity_the_aty);4 5 Intent i =getIntent();6 Bundle data=http://www.mamicode.com/i.getExtras();7 tv=(TextView) findViewById(R.id.tv);8 tv.setText(String.format("name=%s,age=%d,depart=%s",data.getString("name"),data.getInt("age"),data.getString("depart")));9 }

个人觉得,最后一行format()中的那一段,类似于C语言中的printf~

 

三,传递一个对象

首先创建一个类User.java

构造方法User()、getter()和setter()需要创建好

 1 public class User implements Serializable{ 2     private String name; 3     private int age; 4  5     public String getName() { 6         return name; 7     } 8  9     public void setName(String name) {10         this.name = name;11     }12 13     public int getAge() {14         return age;15     }16 17     public void setAge(int age) {18         this.age = age;19     }20     public User(String name, int age){21         this.name=name;22         this.age=age;23     }24 }

 

同样的,new的一个新的类压入到Intent i中去

然后通过后面一个activity中getIntent()出来

MainActivity动作:

 1 protected void onCreate(Bundle savedInstanceState) { 2         super.onCreate(savedInstanceState); 3         setContentView(R.layout.activity_main); 4         findViewById(R.id.btnStartAty).setOnClickListener(new View.OnClickListener() { 5             @Override 6             public void onClick(View v) { 7                 Intent i= new Intent(MainActivity.this,TheAty.class); 8                9                 i.putExtra("user",new User("SWWWW",21));10                 startActivity(i);11 12             }13         });14     }

TheAty动作:

private TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_the_aty);        Intent i =getIntent();//        Bundle data=http://www.mamicode.com/i.getExtras();            tv=(TextView) findViewById(R.id.tv);//        //tv.setText(i.getStringExtra("date"));//        tv.setText(String.format("name=%s,age=%d,depart=%s",data.getString("name"),data.getInt("age"),data.getString("depart")));        User user= (User) i.getSerializableExtra("user");        tv.setText(String.format("User info(name=%s, age=%d)",user.getName(),user.getAge()));    }

 

Activity之间数据传递(一)(简单传递,数据包Bundle)