Android 四大组件之 Activity(2)--传值
来源:互联网 发布:东风标致3008数据 编辑:程序博客网 时间:2024/05/17 07:39
Activity 之间的数据传输
1. Intent 直接传值
第一个页面传值的方法
Intent intent = new Intent(); intent.putExtra("one", num1); intent.putExtra("str", str); intent.setClass(FirstActivity.this, SecondActivity.class); startActivity(intent);
第二个页面取值的方法
Intent intent = getIntent(); int num = intent.getIntExtra("one", -1); String string = intent.getStringExtra("str");
2. Intent 传 Bundle, 由 Bundle 来携带
第一个页面传值的方法
Bundle bundle = new Bundle(); bundle.putString("str", str); bundle.putInt("one", num); Intent intent = new Intent(); intent.putExtras(bundle); intent.setClass(FirstActivity.this, SecondActivity.class); startActivity(intent);
第二个页面取值的方法
Bundle bundle = getIntent().getExtras(); String str = bundle.getString("str"); int num = bundle.getInt("one");
3. 用 startActivityForResult() 来跳转到下一个页面, 在第二个页面 setResult(), 然后 finish() ,第一个页面的 onActivityResult() 接受回调
例如:页面A的局部信息需要修改时,需要跳到页面B来修改
页面A跳转到页面B
写法:
Intent in=new Intent(); in.setClass(Login.this,Regist.class); startActivityForResult(in, 1);//requestCode(请求编号)这个编号为有那个页面跳转回这个页面的(因为有可能有多个页面跳转到这个页面A)
页面B返回跳转到页面A
写法:
Intent in=new Intent(); in.setClass(Regist.this, Login.class); in.putExtra("username", username); in.putExtra("password", password); setResult(Activity.RESULT_OK, in); finish();
页面A的回调方法去接收回调的数据
写法:
protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case 1: String username=data.getStringExtra("username"); String password=data.getStringExtra("password"); usernameEt.setText(username); passwordEt.setText(password); break; default: break; } }
0 0
- Android 四大组件之 Activity(2)--传值
- Android四大组件之Activity
- Android四大组件之Activity
- Android四大组件之Activity
- Android四大组件之Activity
- android四大组件之Activity
- android四大组件之Activity
- android四大组件之Activity
- android四大组件之Activity
- android四大组件之 Activity
- Android四大组件之Activity
- Android四大组件之Activity
- Android四大组件之Activity
- Android四大组件之Activity
- Android四大组件之Activity
- Android 四大组件之Activity
- android 四大组件之activity
- Android 四大组件之 Activity
- C语言函数 bzero
- 有关MVC的一点理解
- JNI之helloworld
- centos 7安装mysql
- column in where clause is ambiguous MySQL异常
- Android 四大组件之 Activity(2)--传值
- [JAVA][2013蓝桥杯预赛 JAVA本科B组][有理数类]
- Nginx 配置文件详解
- 算法系列之十二:多边形区域填充算法--递归种子填充算法
- python中装饰器与元类的使用
- compute服务重启时清除多余的ovs桥
- 4、cas Server连接mysql
- 什么是位、字节、字、KB、MB?
- [JAVA][2013蓝桥杯预赛 JAVA本科B组][三部排序]