Đây là từ Android Developers: Menu -Android Developers
đơn Ngữ cảnh là một trình đơn thả nổi xuất hiện khi người dùng thực hiện một chặng đường dài-click vào một phần tử. Nó cung cấp các hành động ảnh hưởng đến nội dung hoặc khung ngữ cảnh đã chọn.
Hãy tưởng tượng rằng bạn muốn có một trình đơn conext trong một listview
//Constants for context menu options
public static final int MENU_MARK = 1;
public static final int MENU_REMOVE = 2;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
...
...
// Specify that your listview has a context menu attached
registerForContextMenu(getListView());
}
// here you create the context menu
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, MENU_MARK, Menu.NONE, "MARK");
menu.add(Menu.NONE, MENU_REMOVE, Menu.NONE, "Remove");
}
// This is executed when the user selects an option
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case MENU_MARK:
mark_item(info.id);
return true;
case MENU_REMOVE:
delete_item(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
Nguồn
2012-02-02 15:48:48