Intent实现页面跳转和传值
Posted 嘉禾世兴
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Intent实现页面跳转和传值相关的知识,希望对你有一定的参考价值。
*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实现页面跳转和传值的主要内容,如果未能解决你的问题,请参考以下文章