接上一篇《andriod studio新建activity》
下面把简单的个人信息作为参数传递给新建的activity:
(1)把第一页的按钮名称改为: PERSONINFO
(2)在第二页新建一个TextView用来显示个人信息参数:
(3)第一个activity的Java代码改为:
package com.example.xiaolai.helloworld; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button btn1; View v1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); v1=getLayoutInflater().inflate(R.layout.activity_sencond,null); btn1=(Button)findViewById(R.id.button_1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent in=new Intent(); in.setClassName(getApplicationContext(),"com.example.xiaolai.helloworld.SencondActivity"); in.putExtra("com.example.xiaolai.helloworld.SencondActivity.name","zhanghw");// in.putExtra("com.example.xiaolai.helloworld.SencondActivity.age",27); Bundle bundle=new Bundle(); bundle.putString("province","Hunan"); in.putExtra("bundle",bundle);// startActivity(in); } }); } }
(4)第二个activity的代码修改为:
package com.example.xiaolai.helloworld; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class SencondActivity extends AppCompatActivity { private TextView argvTv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sencond); argvTv=(TextView)findViewById(R.id.argvTextView); Intent intent=getIntent(); String name=intent.getStringExtra("com.example.xiaolai.helloworld.SencondActivity.name"); int age=intent.getIntExtra("com.example.xiaolai.helloworld.SencondActivity.age",0); Bundle bundle=intent.getBundleExtra("bundle"); String province=bundle.getString("province"); argvTv.setText("name:"+name+"\nage:"+age+"\n省份:"+province); } }(5)编译运行: