android activity之间的传值

来源:互联网 发布:ubuntu 17.04 iso安装 编辑:程序博客网 时间:2024/06/13 18:27

A 传值activity  B接收值得activity

声明activity类后在在主配置文件中配置新创建的activity,最少需要配置它的name属性值,

在A的布局文件中声明一个Edittext 和一个button 并注册button的单击事件

 <EditText        android:id="@+id/firstEdit"        android:layout_width="fill_parent"        android:layout_height="wrap_content" />    <Button        android:id="@+id/firstButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="click"        android:text="点击" />


在A的按钮点击事件中获取edittext的值,并传个B

public void click(View view) {EditText firstEditText = (EditText) this.findViewById(R.id.firstEdit);String content = firstEditText.getText().toString().trim();Intent intent = new Intent(this, DemoActivity.class);intent.putExtra("name", content);// 基本数据类和基本数据类型的数组都可以用intent传递/** * 批量传递 */Bundle bundle = new Bundle();bundle.putString("hxy", "hhhhh");intent.putExtras(bundle);startActivity(intent);}


在B中的oncreate方法中的Bundle参数接收上一个activity(A)传来的值

@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_demo);Intent intent = getIntent();String name = intent.getStringExtra("name");TextView textView = (TextView) findViewById(R.id.secendText);Bundle bundle = intent.getExtras();String str = bundle.getString("hxy");textView.setText(name + ":" + str);}
原创粉丝点击