13-7-30 上下文菜单(contextMenu)

来源:互联网 发布:手机主板测试软件 编辑:程序博客网 时间:2024/06/03 15:56

上下文菜单(Context Menu)是android.View.Menu的子类,它提供了菜单的设计接口。

官方提供的方法有:

Public Methodsabstract voidclearHeader()
Clears the header of the context menu.
abstract ContextMenusetHeaderIcon(Drawable icon)
Sets the context menu header's icon to the icon given in icon Drawable.
abstract ContextMenusetHeaderIcon(int iconRes)
Sets the context menu header's icon to the icon given in iconRes resource id.
abstract ContextMenusetHeaderTitle(CharSequence title)
Sets the context menu header's title to the title given in title.
abstract ContextMenusetHeaderTitle(int titleRes)
Sets the context menu header's title to the title given in titleRes resource identifier.
abstract ContextMenusetHeaderView(View view)
Sets the header of the context menu to the View given in view.

下面我将做一个单击listView中的item调出ContextMenu,实现:删除、复制、分享到微博,的menu

 

 

其中比较关键的一点就是如何通过listView中的item进行注册ContextMenu,代码很少如下:

private void msg_opearte(){listMsg.setOnItemClickListener(new OnItemClickListener(){@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {// TODO Auto-generated method stub/*TextView txt = (TextView)arg1.findViewById(R.id.chatting_content_itv);msg = txt.getText().toString();Cursor cursor_msg = sqlMsg.findAll();cursor_msg.moveToFirst();cursor_msg.move(arg2-1);Log.i(TAG, arg2+"");msg_position = cursor_msg.getInt(0);Log.i(TAG,"-------->>>>"+msg_position);*/registerForContextMenu(arg0);}});}@Overridepublic void onCreateContextMenu(ContextMenu menu, View V, ContextMenuInfo menuInfo){//添加菜单选项menu.add(0, 1, 0, "删除消息");menu.add(0, 2, 0, "复制消息");menu.add(0, 3, 0, "分享到微博");}@Overridepublic boolean onContextItemSelected(MenuItem item){//添加选项处理switch(item.getItemId()){case 1:sqlMsg.delete(msg_position);refresh_msg();break;case 2:ClipboardManager clip = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); clip.setText(msg); // 复制 break;case 3:break;}return true;}