Android菜单使用之ContextMenu(上下文菜单)

来源:互联网 发布:网络爬虫原理 编辑:程序博客网 时间:2024/05/16 06:21

       在Android应用中,ContextMenu的使用十分普遍,尤其与Listview搭配使用的情况很多。长按一个控件或Listview的一项,会弹出ContextMenu。

     一般通过以下三个方法实现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;@Overrideprotected 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);//注册ContextMenuregisterForContextMenu(lv_contact);}//创建ContextMenu方法@Overridepublic 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处理方法@Overridepublic 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



选择删除某一项之后



0 0