上下文菜单(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;
}