来源: [转载]Android–Activity中使用Intent传值 – 承香墨影 – 博客园
Intent,又称为意图,是一种运行时绑定机制,它能在程序运行的过程中链接两个不同的组件(Activity、Service、 BroadcastReceiver)。通过Intent,程序可以向Android表达某种请求或意愿,Android会根据意愿的内容选择适当的组件 来请求。
在这些组件之间的通讯中,主要是由Intent协助完成的。Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描 述,Android则根据此Intent的描述,负责找到对应的组件,将Intent传递给调用的组件,并完成组件的调用。因此,Intent在这里起着 一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。
- action:表示这个Intent此次操作的动作。
- data:表示这次动作涉及的数据。
通过一个例子来展示Activity中使用Intent导向新Activity并传递数据。此程序仅在两个页面之间相互跳转,但是每次跳转会创 建新的Activity,所以在startActivity()之后需要调用finish()销毁当前Activity,如果不销毁,多次跳转后,程序的 Activity栈中会存放多个Activity,点击设备的返回按钮,会发现会一直向后退。
主要步骤:
- 新建Android项目,增加新布局文件other.xml,新增Activity类otherActivity.class,用于接受Intent并展示other.xml。
- 在MainActivity类中,声明一个Intent类,通过Intent的构造函数指明源和目标。
- 获得Intent后,使用Intent.putExtra()方法对其传入数据。
- 调用Activity.startActivity启动这个Intent。
- 在otherActivity类中,使用Activity.getIntent()获得当前Activity的Intent。
- 获得Intent后,使用Intent.getXxxExtra()方法获得其中保存的数据。
- 在AndroidManifest.xml配置otherActivity节点。
示例代码
步骤2–3:
1 public class MainActivity extends Activity {
2 private TextView textView;
3 private Button btn;
4 @Override
5 protected void onCreate(Bundle savedInstanceState) {
6 super.onCreate(savedInstanceState);
7 setContentView(R.layout.activity_main);
8
9 textView=(TextView)findViewById(R.id.textView1);
10 btn=(Button)findViewById(R.id.button1);
11 btn.setOnClickListener(new View.OnClickListener() {
12 @Override
13 public void onClick(View v) {
14 // Intent构造函数:Intent来源;Intent目的。
15 Intent intent =new Intent(MainActivity.this,otherActivity.class);
16 intent.putExtra("data", "当前是页面2,信息来自页面1");
17 startActivity(intent);//启动Activity
18 finish();
18 }
19 });
20 }
21 }
步骤4–5:
1 public class otherActivity extends Activity {
2 private Button btn;
3 private TextView textView;
4 @Override
5 protected void onCreate(Bundle savedInstanceState) {
6 // TODO Auto-generated method stub
7 super.onCreate(savedInstanceState);
8 setContentView(R.layout.other);
9
10 textView=(TextView)findViewById(R.id.textView2);
11 btn=(Button)findViewById(R.id.button2);
12
13 //通过Activity.getIntent()获取当前页面接收到的Intent。
14 Intent intent =getIntent();
15 //getXxxExtra方法获取Intent传递过来的数据
16 String msg=intent.getStringExtra("data");
17 textView.setText(msg);
18
19 btn.setOnClickListener(new View.OnClickListener() {
20 @Override
21 public void onClick(View v) {
22 Intent intent=new Intent(otherActivity.this,MainActivity.class);
23 startActivity(intent);
24 finish();
25 }
26 });
27 }
28 }
步骤7:
1 <application 2 android:allowBackup="true" 3 android:icon="@drawable/ic_launcher" 4 android:label="@string/app_name" 5 android:theme="@style/AppTheme" > 6 <activity 7 android:name="cn.bgxt.IntentForAc.MainActivity" 8 android:label="@string/app_name" > 9 <intent-filter> 10 <action android:name="android.intent.action.MAIN" /> 11 <category android:name="android.intent.category.LAUNCHER" /> 12 </intent-filter> 13 </activity> 14 <activity android:name=".otherActivity"/> 15 </application>
从Activity中返回数据
上面例子中只是介绍了Activity通过Intent传递数据,然而在实际应用中,不仅仅需要向Activity传递数据,而且要从Activity中返回数据,虽然返回数据和传递数据类似,但是还是有部分区别。
主要区别如下:
- 传递数据需要使用Activity.startActivityForResult()方法启动Activity,需要传递请求码,而不是Activity.startActivity()。
- 返回数据的时候,调用Activity.setResult()方法设置返回Intent以及返回码。
- 需要重写源Activity的onActivityResult()方法以便于接受返回的Intent,在onActivityResult()中会判断请求码和响应码。
通过一个例子说明从Activity返回数据。此程序有两个Activity,在MainActivity中输入加法运算的计算数,跳转到otherActivity中输入计算结果,并在点击返回后,把计算结果输出到MainActivity中。
示例代码
MainActivity:
1 public class MainActivity extends Activity {
2
3 private EditText one,two,result;
4 private Button btn;
5
6 @Override
7 protected void onCreate(Bundle savedInstanceState) {
8 super.onCreate(savedInstanceState);
9 setContentView(R.layout.activity_main);
10
11 one=(EditText)findViewById(R.id.one);
12 two=(EditText)findViewById(R.id.two);
13 result=(EditText)findViewById(R.id.result);
14 btn=(Button)findViewById(R.id.btnGo);
15 btn.setOnClickListener(new View.OnClickListener() {
16
17 @Override
18 public void onClick(View v) {
19 // TODO Auto-generated method stub
20 int ione=Integer.parseInt(one.getText().toString());
21 int itwo=Integer.parseInt(two.getText().toString());
22
23 Intent intent=new Intent(MainActivity.this, otherActivity.class);
24 intent.putExtra("one", ione);
25 intent.putExtra("two", itwo);
26
27 //启动需要监听返回值的Activity,并设置请求码:requestCode
28 startActivityForResult(intent, 1);
29 }
30 });
31
32 }
33
34 @Override
35 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
36 super.onActivityResult(requestCode, resultCode, data);
37 //当otherActivity中返回数据的时候,会响应此方法
38 //requestCode和resultCode必须与请求startActivityForResult()和返回setResult()的时候传入的值一致。
39 if(requestCode==1&&resultCode==2)
40 {
41 int three=data.getIntExtra("three", 0);
42 result.setText(String.valueOf(three));
43 }
44 }
45
46 @Override
47 public boolean onCreateOptionsMenu(Menu menu) {
48 // Inflate the menu; this adds items to the action bar if it is present.
49 getMenuInflater().inflate(R.menu.main, menu);
50 return true;
51 }
52 }
otherActivity:
1 public class otherActivity extends Activity {
2 TextView tvShow;
3 EditText etResult;
4 @Override
5 protected void onCreate(Bundle savedInstanceState) {
6 super.onCreate(savedInstanceState);
7 setContentView(R.layout.other);
8
9 tvShow=(TextView)findViewById(R.id.tvShow);
10 etResult=(EditText)findViewById(R.id.etResult);
11
12 Intent intent=getIntent();
13 int a=intent.getIntExtra("one", 0);
14 int b=intent.getIntExtra("two", 0);
15 tvShow.setText(a+" + "+b+" = "+" ? ");
16
17 Button btnResult=(Button)findViewById(R.id.btnReturn);
18 btnResult.setOnClickListener(new View.OnClickListener() {
19 @Override
20 public void onClick(View v) {
21 //新声明一个Intent用于存放放回的数据
22 Intent i=new Intent();
23 int result=Integer.parseInt(etResult.getText().toString());
24 i.putExtra("three", result);
25 setResult(2, i);//设置resultCode,onActivityResult()中能获取到
26 finish();//使用完成后结束当前Activity的生命周期
27 }
28 });
29 }
30 }
Mikel
