我的Android学习笔记之二:Activity间的传递
刚刚学习了Android不久,共粗略地学习了几个概念:Activity、Intent、Bundle,写了十分简单一个小程序,这个小程序中共有两个Activity类,用Intent实现它们间的跳转,用Bundle传递数据。
首先先介绍:Activity、Intent、Bundle
Activity的主要作用:
- Activity是Android应用程序的一个用户接口,用户和应用程序直接进行交互的接口。
- Activity实际上是一个各种控件的一个容器,我们把控件摆放进去。
Intent的作用:消息触发、消息传递、消息响应。
具体地说:Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的 组件,将Intent传递给调用的组件,并完成组件的调用。因此,Intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。
Bundle的作用:
Bundle类用作携带数据,它类似于Map,用于存放key-value名值对形式的值。
Toast的作用:
Toast用于向用户显示一些帮助/提示,别忘了调用show()方法。
当然,这三个类还有许多东西要学,特别是Activity,既然如此,我只能够后面再深入学咯。
在类包中添加两个类:HelloActivity和ReflectActivity。
HelloActivity代码:
1 public class HelloActivity extends Activity
2
3 {
4
5 private Button mainBtn=null;
6
7 private final static int REQUEST_CODE=1;
8
9
10
11 /** Called when the activity is first created. */
12
13 @Override
14
15 public void onCreate(Bundle savedInstanceState)
16
17 {
18
19 super.onCreate(savedInstanceState);
20
21 setContentView(R.layout.main);
22
23 mainBtn=(Button)findViewById(R.id.button1);
24
25 mainBtn.setOnClickListener(listener);
26
27 }
28
29
30
31 private OnClickListener listener=new OnClickListener()
32
33 {
34
35 @Override
36
37 public void onClick(View v)
38
39 {
40
41 Intent intent=new Intent();
42
43 intent.setClass(HelloActivity.this, ReflectActivity.class);
44
45 intent.putExtra("str", "你知道吗?");
46
47 startActivityForResult(intent, REQUEST_CODE);
48
49 }
50
51
52
53 };
54
55
56
57 @Override
58
59 protected void onActivityResult(int requestCode, int resultCode, Intent data)
60
61 {
62
63 if (requestCode==REQUEST_CODE)
64
65 {
66
67 if (resultCode==ReflectActivity.RESULT_CODE)
68
69 {
70
71 Bundle bundle=data.getExtras();
72
73 String str=bundle.getString("back");
74
75 Toast.makeText(HelloActivity.this, str, Toast.LENGTH_LONG).show();
76
77 }
78
79 }
80
81 }
82
83 }
ReflectActivity代码:
1 public class ReflectActivity extends Activity
2
3 {
4
5 public final static int RESULT_CODE=1;
6
7 private TextView Txt;
8
9 private EditText editText;
10
11 private Button secondBtn;
12
13
14
15 @Override
16
17 protected void onCreate(Bundle savedInstanceState)
18
19 {
20
21 super.onCreate(savedInstanceState);
22
23 setContentView(R.layout.reflect);
24
25 Intent intent=getIntent();
26
27 Bundle bundle=intent.getExtras();
28
29 String str=bundle.getString("str");
30
31 Txt=(TextView)findViewById(R.id.text2);
32
33 Txt.setText(str);
34
35
36
37 secondBtn=(Button)findViewById(R.id.button2);
38
39 secondBtn.setOnClickListener(listener);
40
41
42
43 }
44
45
46
47 private OnClickListener listener=new OnClickListener()
48
49 {
50
51 @Override
52
53 public void onClick(View v)
54
55 {
56
57 Intent intent=new Intent();
58
59 intent.putExtra("back", "我知道…");
60
61 setResult(RESULT_CODE, intent);
62
63 finish();
64
65 }
66
67
68
69 };
70
71 }
在Layout中添加main.xml和reflect.xml(其实main.xml已经自动添加)
main.xml代码:(用于HelloActivity布局)
1 <?xml version="1.0" encoding="utf-8"?>
2
3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4
5 android:layout_width="fill_parent"
6
7 android:layout_height="fill_parent"
8
9 android:orientation="vertical" >
10
11
12
13 <TextView
14
15 android:id="@+id/text"
16
17 android:layout_width="fill_parent"
18
19 android:layout_height="wrap_content"
20
21 android:text="Hello,what do you want to talk to me?"
22
23 />
24
25 <EditText
26
27 android:id="@+id/edit"
28
29 android:layout_width="fill_parent"
30
31 android:layout_height="wrap_content"
32
33 android:background="@drawable/shape"
34
35 android:text="你知道吗??"
36
37 />
38
39 <Button
40
41 android:id="@+id/button1"
42
43 android:layout_width="wrap_content"
44
45 android:layout_height="wrap_content"
46
47 android:text="click"
48
49 />
50
51 </LinearLayout>
reflect.xml代码:(用于ReflectActivity布局)
1 <?xml version="1.0" encoding="utf-8"?>
2
3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4
5 android:layout_width="fill_parent"
6
7 android:layout_height="fill_parent"
8
9 android:orientation="vertical" >
10
11
12
13 <TextView
14
15 android:id="@+id/text2"
16
17 android:layout_width="fill_parent"
18
19 android:layout_height="wrap_content"
20
21 android:text="Of cource"
22
23 />
24
25 <EditText
26
27 android:id="@+id/reflect"
28
29 android:layout_width="fill_parent"
30
31 android:layout_height="wrap_content"
32
33 android:background="@drawable/shape"
34
35 android:text="我知道..."
36
37 />
38
39
40
41 <Button
42
43 android:id="@+id/button2"
44
45 android:layout_width="wrap_content"
46
47 android:layout_height="wrap_content"
48
49 android:text="reflect"
50
51 />
52
53 </LinearLayout>
54
55
修改AndroidManiFest.xml
AndroidManiFest.xml代码:
1 <?xml version="1.0" encoding="utf-8"?>
2
3 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
4
5 package="com.shen.android.learning"
6
7 android:versionCode="1"
8
9 android:versionName="1.0" >
10
11
12
13 <uses-sdk android:minSdkVersion="8" />
14
15
16
17 <application
18
19 android:icon="@drawable/ic_launcher"
20
21 android:label="@string/app_name" >
22
23 <activity
24
25 android:name=".HelloActivity"
26
27 android:label="@string/app_name" >
28
29 <intent-filter>
30
31 <action android:name="android.intent.action.MAIN" />
32
33
34
35 <category android:name="android.intent.category.LAUNCHER" />
36
37 </intent-filter>
38
39 </activity>
40
41 <activity
42
43 android:name=".ReflectActivity"
44
45 android:label="@string/app_name" >
46
47 </activity>
48
49 </application>
50
51 <uses-permissionandroid:name="android.permission.CALL_PHONE"/>
52
53 <uses-permission android:name="android.permission.SEND_SMS"/>
54
55 </manifest>
其实只需要添加以下几行:
<activity
android:name=".ReflectActivity"
android:label="@string/app_name" >
</activity>
效果图:



浙公网安备 33010602011771号