Android菜单使用之ContextMenu(上下文菜单)
2014-02-25 10:46
435 查看
在Android应用中,ContextMenu的使用十分普遍,尤其与Listview搭配使用的情况很多。长按一个控件或Listview的一项,会弹出ContextMenu。
一般通过以下三个方法实现ContextMenu的使用。
1.为控件注册ContextMenu
2.重写创建ContextMenu方法
3.重写选择ContextMenu方法
其他的一些方法可参见官方API:http://developer.android.com/reference/android/view/ContextMenu.html
以下是个人写的一个Listview使用ContextMenu的小例子,功能非常简单。点击Listview的某一项弹出ContextMenu,选择删除选项,删除Listview的当前项。
Listview的一行布局item.xml
主界面
MainActivity.java
效果图
![](https://img-blog.csdn.net/20140225104941078?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd3lpaTI2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
长按一项弹出ContextMenu
![](https://img-blog.csdn.net/20140225104951328?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd3lpaTI2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
选择删除某一项之后
一般通过以下三个方法实现ContextMenu的使用。
1.为控件注册ContextMenu
public void registerForContextMenu (View view)
2.重写创建ContextMenu方法
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
3.重写选择ContextMenu方法
public boolean onContextItemSelected(MenuItem item)
其他的一些方法可参见官方API:http://developer.android.com/reference/android/view/ContextMenu.html
以下是个人写的一个Listview使用ContextMenu的小例子,功能非常简单。点击Listview的某一项弹出ContextMenu,选择删除选项,删除Listview的当前项。
Listview的一行布局item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="3dp" android:orientation="horizontal" > <ImageView android:id="@+id/imageView1" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/find" /> <LinearLayout android:layout_marginLeft="5dp" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="姓名" /> <TextView android:layout_marginTop="5dp" android:id="@+id/tv_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1500000000" /> </LinearLayout> </LinearLayout>
主界面
MainActivity.java
/** * * @author ElaineWong * */ public class MainActivity extends ListActivity { private Context mContext; private ListView lv_contact; private SimpleAdapter sa_contact; private List<HashMap<String,Object>> contactList; private final int MENU_DELETE=0,MENU_EDIT=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext=this; initView(); initData(); } private void initView(){ lv_contact=getListView(); } private void initData(){ contactList=new ArrayList<HashMap<String,Object>>(); for(int i=0;i<20;i++){ HashMap<String,Object> map=new HashMap<String, Object>(); map.put("name", "姓名"+i); map.put("phone", "15312001080"); map.put("icon",R.drawable.find); contactList.add(map); } String[] from={"name","phone","icon"}; int[] to={R.id.tv_name,R.id.tv_phone,R.id.imageView1}; sa_contact=new SimpleAdapter(mContext, contactList, R.layout.item, from, to); lv_contact.setAdapter(sa_contact); //注册ContextMenu registerForContextMenu(lv_contact); } //创建ContextMenu方法 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { try { AdapterContextMenuInfo mMenuInfo = (AdapterContextMenuInfo) menuInfo; menu.setHeaderTitle(contactList.get(mMenuInfo.position).get("name").toString()); menu.setHeaderIcon(R.drawable.find); } catch (Exception e) { return; } int groupId=0; menu.add(groupId,MENU_DELETE, 0, "删除联系人"); menu.add(groupId,MENU_EDIT, 1, "编辑联系人"); super.onCreateContextMenu(menu, v, menuInfo); } //点击ContextMenu处理方法 @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo itemInfo = (AdapterContextMenuInfo) item.getMenuInfo(); switch (item.getItemId()) { case MENU_DELETE: Toast.makeText(mContext,item.getTitle()+":"+contactList.get(itemInfo.position).get("name").toString(), Toast.LENGTH_LONG).show(); contactList.remove(itemInfo.position); sa_contact.notifyDataSetChanged(); break; case MENU_EDIT: Toast.makeText(mContext,item.getTitle()+":"+contactList.get(itemInfo.position).get("name").toString(), Toast.LENGTH_LONG).show(); break; default: break; } return super.onContextItemSelected(item); } }
效果图
长按一项弹出ContextMenu
选择删除某一项之后
相关文章推荐
- Android菜单详解(四)——使用上下文菜单ContextMenu
- Android菜单详解(四)——使用上下文菜单ContextMenu
- Android菜单详解(四)——使用上下文菜单ContextMenu
- Android菜单详解(四)——使用上下文菜单ContextMenu
- Android菜单详解(四)——使用上下文菜单ContextMenu
- Android入门(45)——第七章 使用ContextMenu实现上下文菜单
- (4.1.1.3)Android 的上下文菜单: ContextMenu的使用方法以及与OptionMenu的区别
- Android基础知识】选项菜单、上下文菜单、子菜单的使用
- Android Menu,OptionMenu选项菜单,ContextMenu上下文菜单,Contextual ActionMode,PopUp Menu,PopU pWi ndow
- Android OptionMenu (选项菜单) 和 ContextMenu (上下文菜单) 、 SubMenu (子菜单)
- Android——菜单之ContextMenu(上下文菜单)
- Android菜单详解——上下文菜单(ContextMenu )
- android应用开发详解(四)----------各种菜单(optionmenu、contextmenu、submenu)和Toast的使用
- Android开发之Menu:OptionMenu(选项菜单)、ContextMenu(上下文菜单)、SubMenu(子菜单)
- Android开发之Menu:OptionMenu(选项菜单)、ContextMenu(上下文菜单)、SubMenu(子菜单)
- 【Android基础知识】选项菜单、上下文菜单、子菜单的使用
- Android中的上下文菜单(ContextMenu)使用案例
- 【Android】使用Popwindow实现左侧弹出菜单层
- android 使用DrawerLayout实现侧滑菜单
- Android之ContextMenu的使用方法以及与OptionMenu的区别(转)