android页面之间跳转的几种方法

android实现跳转的几种方法:

发送方:
1、显式
(1):
Intent intent = new Intent();
定义组件名对象,(当前Activity的实例,要跳转的Activity类)
ComponentName cn = new ComponentName(MainActivity.this,SecActivity.class);
ComponentName cn= new ComponentName(MainActivity.class,SecActivity.class);
intent.setComponent(cn);
startActivity(intent);

(2):
Intent intent = new Intent();
intent.setClass(MainActivity.this,SecActivity.class);
startActivity(intent);

(3):
Intent intent = new Intent();
intent.setClassName(MainActivity.this,SecActivity.classgetName());
startActivity(intent);

2、隐式
Intent intent = new Intent(MainActivity.this,SecActivity.class);
startActivity(intent);

(1):
Intent对象附加数据
String userName = aet01.getText().toString();
Intent intent = new Intent(MainActivity.this,SecActivity.class);

MainActivity.this数据发送方,SecActivity.class数据接收方

intent.putExtra(“inName”, userName);
intent.putExtra(“flagNum”, 66);

附加数据

startActivity(intent);

(2):借助第三方对象Bundle对象附加数据,再将Bundle对象附加到Intent对象上
Bundle是一个容器存放键值对,键必须是String类型,而且唯一。
String userName = aet01.getText().toString();
Intent intent = new Intent(MainActivity.this,SecActivity.class);
Bundle bundle = new Bundle();
bundle.putInt(“flagNum”, 66);
bundle.putString(“inName”, userName);
intent.putExtras(bundle); //注意方法名的写法
startActivity(intent);
接收方:获取传过来的intent对象
(1):
Intent aintent = getIntent();
if(aintent!=null){
int flagNum = aintent.getIntExtra(“flagNum”, 0);//0是默认值
String username = aintent.getStringExtra(“inName”);
String strShow = “用户名:”+username+"\n"+“标志数字:”+flagNum;
Toast.makeText(SecActivity.this, strShow, 1000).show();
}else{

}
(2):
Intent aintent = getIntent();
if(aintent!=null){
Bundle abundle = aintent.getExtras();
int flagNum = abundle.getInt(“flagNum”);
String username = abundle.getString(“inName”);
TextView sectv01 = (TextView)findViewById(R.id.sectv01);
sectv01.setText(“用户名:”+username+"\n"+“标志数字:”+flagNum);
}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值