Thực hành Context menu trong Android
Hôm nay mình sẽ giới thiệu cho các bạn bước cơ bản về context menu trong Android. Việc đầu tiên là chúng ta phải cũng phải đăng ký Context Menu cho đối tượng sau đó muốn hiển thị lên thì ta nhấn thật lâu vào đối tượng (long click ).
>>>> Trước đó bạn nên đọc: Context là gì?
Thực hành context menu trong Android
Vntalking cùng các bạn sẽ làm một app nhỏ với một TextView và Button, khi ấn và giữ vào TextView hay Button sẽ hiện ra một ContextMenu.
Giao diện chương trình đây:
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
| android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${packageName}.${activityClass}" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_margin="10dp" android:layout_marginTop="15dp" android:padding="4dp" android:text="Xem thiết bị" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/text" android:layout_below="@+id/text" android:layout_marginTop="52dp" android:text="Cập nhật" /></RelativeLayout> |
Tiếp là file Class
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
72
73
74
75
76
77
78
79
| package opin.contextmenu;import android.app.Activity;import android.os.Bundle;import android.view.ContextMenu;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ContextMenu.ContextMenuInfo;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity { private TextView text; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView) findViewById(R.id.text); button = (Button) findViewById(R.id.button); // Đăng ký ContextMenu cho textView và Button // Sau khi đăng ký, nếu ấn và giữ vào text hoặc button sẽ hiển thị menu registerForContextMenu(text); registerForContextMenu(button); } // Phương thức này được gọi khi ấn và giữ vào text hoặc button @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){ super.onCreateContextMenu(menu, v, menuInfo); // ến ấn và giữ vào text thì hiện menu theo file context_menu_text if(v == text){ getMenuInflater().inflate(R.menu.context_menu_text, menu); } // ến ấn và giữ vào Button thì hiện menu theo file context_menu_button if(v == button){ getMenuInflater().inflate(R.menu.context_menu_button, menu); } } // Phương thức này sẽ được gọi khi chọn một phần tử trong ContextMennu @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.itemInfor: Toast.makeText(this, "Bạn vừa chọn Xem thông tin thiết bị", Toast.LENGTH_SHORT).show(); break; case R.id.itemMAC: Toast.makeText(this, "Bạn vừa chọn Xem địa chỉ Mac", Toast.LENGTH_SHORT).show(); break; case R.id.itemAndroid: Toast.makeText(this, "Bạn vừa chọn Xem phiên bản Android", Toast.LENGTH_SHORT).show(); break; case R.id.itemDelete: Toast.makeText(this, "Bạn vừa chọn Xóa", Toast.LENGTH_SHORT).show(); break; case R.id.itemSelect: Toast.makeText(this, "Bạn vừa chọn 'Chọn' ", Toast.LENGTH_SHORT).show(); break; case R.id.itemUpdate: Toast.makeText(this, "Bạn vừa chọn Sửa", Toast.LENGTH_SHORT).show(); break; default: break; } return super.onContextItemSelected(item); }} |
Trong class chính các bạn thấy mình viết đè phương thức onCreateContextMenu, phương thức này sẽ được gọi khi ấn và giữ vào TextView hoặc Button, thao tác này được “đăng ký” qua lời gọi
registerForContextMenu(text);
registerForContextMenu(button)
registerForContextMenu(text);
registerForContextMenu(button)
Trong phương thức onCreateContextMenu các bạn thấy có hai lệnh if, nếu ấn giữ vào text thì hiện một file giao diện menu khác, nếu ấn giữ vào button sẽ hiện một gio diện menu khác.
Để hiển thị ContextMenu ta sử dụng phương thức:
getMenuInflater().inflate(R.menu.context_menu_text, menu);
Trong đó R.menu.context_menu_text là file context_menu_text.xml (file chứa giao diện của menu), tương tự vậy, ta cũng có R.menu.context_menu_button.
File context_menu_text.xml nằm trong thư mục res/menu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| <?xml version="1.0" encoding="utf-8"?> <item android:id="@+id/itemInfor" android:title="Thông tin thiết bị"/> <item android:id="@+id/itemMAC" android:title="Địa chỉ MAC"/> <item android:id="@+id/itemAndroid" android:title="Phiên bản Android"/></menu> |
Đây là file context_menu_button.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| <?xml version="1.0" encoding="utf-8"?> <item android:id="@+id/itemDelete" android:title="Xóa"/> <item android:id="@+id/itemSelect" android:title="Chọn"/> <item android:id="@+id/itemUpdate" android:title="Sửa"/></menu> |
Code mẫu ở bên dưới nhé các bạn:
https://www.dropbox.com/sh/tmcm453tp9k8yp9/AABRjfgP8INW7_Y5I3cF1jo7a
https://www.dropbox.com/sh/tmcm453tp9k8yp9/AABRjfgP8INW7_Y5I3cF1jo7a
À đừng quên khóa học Android miễn phí ở đây nhé: https://vntalking.blogspot.com/2018/07/cac-khoa-hoc-lap-trinh-mien-phi-ma-ban.html
Nguồn tham khảo:
ContextMenu trong Android https://nguyenkhoaninh.wordpress.com/2014/07/07/androidbasic-bai2-contextmenu-trong-android/


Nhận xét
Đăng nhận xét