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

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

官方提供的方法有:

Public Methods
abstract void clearHeader()
Clears the header of the context menu.
abstract   ContextMenu setHeaderIcon( Drawable  icon)
Sets the context menu header's icon to the icon given in   icon   Drawable.
abstract   ContextMenu setHeaderIcon(int iconRes)
Sets the context menu header's icon to the icon given in   iconRes  resource id.
abstract   ContextMenu setHeaderTitle( CharSequence  title)
Sets the context menu header's title to the title given in   title.
abstract   ContextMenu setHeaderTitle(int titleRes)
Sets the context menu header's title to the title given in   titleRes  resource identifier.
abstract   ContextMenu setHeaderView( 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(){

			@Override
			public 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);
			}			
		});
	}
	@Override
	public void onCreateContextMenu(ContextMenu menu, View V, ContextMenuInfo menuInfo){
		//添加菜单选项
		menu.add(0, 1, 0, "删除消息");
		menu.add(0, 2, 0, "复制消息");
		menu.add(0, 3, 0, "分享到微博");
	}
	@Override
	public 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;		
	}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值