Bài 24: Intent Trong Android(P3)

Leave a Comment
Ví dụ 2: Mở một Activity khác đồng thời truyền dữ liệu qua.
- Bạn xem hình Tôi minh họa dưới này:
24_intent_4
- Ở đây Ta thường dùng đối tượng Bundle để đóng gói dữ liệu để truyền tải qua các Activity khác.
- Tại sao lại nên dùng Bundle để truyền tải dữ liệu qua lại giữa các Activity?
+ Tối lấy ví dụ sau: Giả sử bạn phải chuyển toàn bộ hàng hóa từ Việt Nam sang Singapore, trường hợp này Ta sẽ bỏ tất cả các hàng hóa vào trong 1 Container rồi chuyển Container này đi, Không phải chuyển từng món hàng 1 qua Singapore. Tương tự như vậy, đối với Android khi truyền dữ liệu chúng ta cũng có thể truyền từng thông số riêng lẻ nhưng chúng ta không nên, chúng ta phải bỏ tất cả các dữ liệu riêng lẻ đó vào 1 Bundle rồi gửi Bundle này qua Activity nào đó. Bên kia chỉ cần lấy Bundle này ra, mọi dữ liệu đều nằm trong Bundle và ta dễ dàng xử lý.
24_intent_5- Bundle có 2 loại phương thức: putXXX dùng để cho bên gửi đi, ứng với kiểu dữ liệu nào thì put đúng kiểu đó. ở ví dụ trên putInt và putDouble là tương ứng với 2 loại dữ liệu khác nhau (phải đi kèm với key để bên nhận có thể xử lý đúng)
- Ở bên nhận Bundle thì dùng các phương thức getXXX tương ứng để lấy dữ liệu theo key bên gửi.
- key này phải giống nhau cho cả 2 nơi. Nơi nhận đặt key=soa thì bên nhận cũng phải dùng key=soa.
- Ta cũng có thể truyền Đối Tượng qua lại giữa các Activity, các đối tượng này phải được Serialize
24_intent_6- Trong trường hợp truyền đối tượng thì ta dùng putSerializable và getSerializable
- Tôi sẽ làm một ví dụ đơn giản với giao diện như bên dưới:
24_intent_7- Từ MainActivity có 2 thông số là a và b. Khi nhấn nút Kết quả nó sẽ truyền 2 thông số này qua ResultActivity và tiến hành giải phương trình bậc 1. Muốn trở về để tiếp tục giải phương trình khác khi nhấn nút Back.
- Bạn xem cấu trúc chương trình để dễ xử lý:
24_intent_8- Bạn xem giao diện của MainActivity (activity_main.xml):
24_intent_9- Đây là Source XML của MainActivity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/LinearLayout1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context=".MainActivity" >
 <TextView
 android:id="@+id/textView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:background="#008040"
 android:gravity="center"
 android:text="Giải phương trình bậc 1"
 android:textColor="#FFFF00" />
 <TableLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:stretchColumns="*"
 >
 <TableRow
 android:id="@+id/tableRow1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" >
 <TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="#E5E5E5"
 android:text="Nhập a:" />
 <EditText
 android:id="@+id/txta"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:inputType="text"
 android:ems="10" >
 <requestFocus />
 </EditText>
 </TableRow>
 <TableRow
 android:id="@+id/tableRow2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" >
 <TextView
 android:id="@+id/textView3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="#E5E5E5"
 android:text="Nhập b:" />
 <EditText
 android:id="@+id/txtb"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:inputType="text"
 android:ems="10" />
 </TableRow>
 <TableRow
 android:id="@+id/tableRow3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 >
 <Button
 android:id="@+id/btnketqua"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_column="1"
 android:text="Kết quả" />
 </TableRow>
 </TableLayout>
</LinearLayout>
- Trong xml layout Bạn có thể chọn layout nào cũng được, ở trên Tôi dùng TableLayout

0 nhận xét:

Đăng nhận xét