Android上下文菜单
GridView gv = (GridView) findViewById(R.id.gv);
// 添加长按点击弹出选择菜单
gv.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuinfo) {
AdapterContextMenuInfo am=(AdapterContextMenuInfo)menuinfo;
//获得每个listview中的item
View item=am.targetView;
TextView tv_id=(TextView) item.findViewById(R.id.tv_id);
TextView tv_title=(TextView)item.findViewById(R.id.tv_title);
menu.setHeaderTitle(tv_title.getText().toString());
//获得要删除的记录id
id=tv_id.getText().toString();
menu.add(0, 0, 0, "更新该条");
menu.add(0, 1, 0, "删除该条");
}
});
上下文菜单被选择了
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
Toast.makeText(MainActivity.this, "你点击了上下文菜单0",
Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(MainActivity.this, "你点击了上下文菜单1",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onContextItemSelected(item);
}