码迷,mamicode.com
首页 > 其他好文 > 详细

Intent实现页面跳转和传值

时间:2016-03-18 13:24:14      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

*Intent称为意图,是Android各大组件连接的桥梁

1.Activity页面跳转

Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);  
MainActivity.this.startActivity(intent);

2.Activity页面跳转传值

第一种方法:

发送方:

Intent intent = new Intent();
intent.putExtra("name", "诸葛亮");
intent.putExtra("age", 50);
intent.putExtra("IQ", 200.0f);
intent.setClass(MainActivity.this, SecondActivity.class);
MainActivity.this.startActivity(intent);

接受方:

Intent intent = getIntent();
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 0);
float IQ = intent.getFloatExtra("IQ", 0.0f);
textview2.setText("name:"+name+",age:"+age+",IQ:"+IQ);

第二种方法:

发送方:

Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("name", "乔峰");
bundle.putInt("age", 40);
bundle.putFloat("weight", 70.4f);
intent.putExtras(bundle);
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);

接受方:

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String name = bundle.getString("name");
int age = bundle.getInt("age");
float weight = bundle.getFloat("weight");
textview.setText(name+","+age+","+weight);

3.页面返回传值

被返回方:

startActivityForResult(intent, 38);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Bundle bundle = data.getExtras();
    String name = bundle.getString("name");
    int age = bundle.getInt("age");
    float weight = bundle.getFloat("weight");
    Toast.makeText(MainActivity.this, name+age+weight, Toast.LENGTH_LONG).show();
    super.onActivityResult(requestCode, resultCode, data);
    }

返回方:

Intent data = new Intent();
data.setClass(SecondActivity.this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name", "张无忌");
bundle.putInt("age", 20);
bundle.putFloat("weight", 120.5f);
data.putExtras(bundle);
setResult(250, data);
finish();

 

Intent实现页面跳转和传值

标签:

原文地址:http://www.cnblogs.com/anni-qianqian/p/5291605.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!