美文网首页
Activity 之间的数据转换

Activity 之间的数据转换

作者: 5e30a667061c | 来源:发表于2018-06-18 20:52 被阅读0次

    Intent putExtra()方法

    示例,Activity A 跳转B
    A:

        Intent intent = new Intent(MainActivity.this,SecActivity.class);
                    intent.putExtra("name","charlie");
                    startActivity(intent);
    

    B:

           String data = getIntent().getStringExtra("name");
            ((TextView)findViewById(R.id.tv)).setText(data);
    

    Bundle 传值

    A:

      Intent intent = new Intent(MainActivity.this,SecActivity.class);
    
                    Bundle bundle = new Bundle();
                    bundle.putString("name","charlie");
                    intent.putExtras(bundle);
                    startActivity(intent);
    

    B:

      Bundle bundle = getIntent().getExtras();
            String data = bundle.getString("name");
            ((TextView)findViewById(R.id.tv)).setText(data);
    

    传递对象

    示例
    Person 对象:

    public class Person implements Serializable{
        private String name;
        private String age ;
    
        public String getAge() {
            return age;
        }
    
        public void setAge(String age) {
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "name=="+name+"::::"+"age==="+age;
        }
    
    

    A:

      Intent intent = new Intent(MainActivity.this,SecActivity.class);
                    Bundle bundle = new Bundle();
                    Person person = new Person();
                    person.setName("ccccc");
                    person.setAge("777");
                    bundle.putSerializable("person",person);
                    intent.putExtras(bundle);
                    startActivity(intent);
    

    B:

       Person person = (Person)getIntent().getExtras().getSerializable("person");
            ((TextView)findViewById(R.id.tv)).setText(person.toString());
    

    相关文章

      网友评论

          本文标题:Activity 之间的数据转换

          本文链接:https://www.haomeiwen.com/subject/pibneftx.html