android studio activity传参

接上一篇《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)编译运行:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶落西湘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值