首页 > 代码库 > Intent传参数

Intent传参数

Intent 是Android 程序中各组件之间进行交互的一种重要方式,它不仅可以指明当前组件想要执行的动作,还可以在不同组件之间传递数据。Intent 一般可被用于启动活动、启动服务、以及发送广播等场景


   // A activity调用        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);       // setContentView(R.layout.activity_main);  // 创建视图        setContentView(R.layout.my_layout);        // 找到对应的button来监听事件        findViewById(R.id.butStartAnotherAty).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent i = new Intent(MainActivity.this, AnotherAty.class);                i.putExtra("data", "hello word");  // 使用Intent来传参                startActivity(i);            }        });        System.out.println("onCreate");    }
    //B activity 通过Intent来获取值,并显示在textView上面    private TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_another_aty);        Intent i = getIntent();  //直接获取传过来的intent        tv = (TextView)findViewById(R.id.textView);        tv.setText(i.getStringExtra("data"));    }

 

 

// 如果数据比较多,可以通过 Bundle  数据包来传递数据

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);       // setContentView(R.layout.activity_main);  // 创建视图        setContentView(R.layout.my_layout);        // 找到对应的button来监听事件        findViewById(R.id.butStartAnotherAty).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent i = new Intent(MainActivity.this, AnotherAty.class);                //i.putExtra("data", "hello word");  // 使用Intent来传参                Bundle b = new Bundle();  // 打包数据                b.putString("name", "chengzhier");                b.putInt("age", 2);                i.putExtras(b);                startActivity(i);            }        });        System.out.println("onCreate");    }private TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_another_aty);        Intent i = getIntent();  //直接获取传过来的intent        tv = (TextView)findViewById(R.id.textView);        //i.getStringExtra("data")        Bundle data =http://www.mamicode.com/ i.getExtras();        String s = String.format("name=%s, age=%d", data.getString("name"), data.getInt("age"));        tv.setText(s);    }

 

 

// 传递一个对象

// User类public class User implements Serializable{  //让这个对象序列化    private String name;    private int age;    public User(int age, String name ) {        this.age = age;        this.name = name;    }    public void setAge(int age) {        this.age = age;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public String getName() {        return name;    }}// A activityprotected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);       // setContentView(R.layout.activity_main);  // 创建视图        setContentView(R.layout.my_layout);        // 找到对应的button来监听事件        findViewById(R.id.butStartAnotherAty).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent i = new Intent(MainActivity.this, AnotherAty.class);                i.putExtra("user", new User(2, "zh"));                startActivity(i);            }        });        System.out.println("onCreate");    }// B activiry private TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_another_aty);        Intent i = getIntent();  //直接获取传过来的intent        tv = (TextView)findViewById(R.id.textView);        User u = (User)i.getSerializableExtra("user");  //取出来                 String s = String.format("测试11name=%s, age=%d", u.getName(), u.getAge());        tv.setText(s);    }

 

Intent传参数