*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();
时间: 2024-12-14 22:51:01