当用户在菜单窗口外单击时,我需要关闭上下文菜单。我该如何实现它?在某些地方,我看到当用户在窗口外点击时,contextmenu会被关闭。或者是否有其他方法可以关闭上下文菜单。请帮助..谢谢。我的代码在这里
selectedImageView = (ImageView) findViewById(R.id.selected_imageview);
registerForContextMenu(selectedImageView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Post Image");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextmenu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.take_photo:
Intent intent = new Intent(this, Mag.class);
this.startActivity(intent);
break;
case R.id.choose_gallery:
Intent intent2 = new Intent(this, Mag1.class);
this.startActivity(intent2);
break;
case R.id.share_cancel:
Intent intent3 = new Intent(this, Mag2.class);
this.startActivity(intent3);
break;
default:
return super.onContextItemSelected(item);
}
return true;
}
答案 0 :(得分:1)
您无需为所需内容实施任何其他代码。
当您点击其区块外
时,Android会自动关闭menu window
答案 1 :(得分:1)
试试这个
public static final int take_photo= Menu.FIRST;
public static final int choose_gallery= Menu.FIRST+1;
public static final int share_cancel= Menu.FIRST+2;
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
menu.add(0,take_photo, 0, "take_photo");
menu.add(0, choose_gallery, 0, "choose_gallery");
menu.add(0, share_cancel, 0, "share_cancel");
}
@Override
public boolean onContextItemSelected(MenuItem item) { ;
onOptionsItemSelected(item);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.take_photo:
Intent intent = new Intent(this, Mag.class);
this.startActivity(intent);
break;
case R.id.choose_gallery:
Intent intent2 = new Intent(this, Mag1.class);
this.startActivity(intent2);
break;
case R.id.share_cancel:
Intent intent3 = new Intent(this, Mag2.class);
this.startActivity(intent3);
break;
default:
return super.onContextItemSelected(item);
}
return true;
}
答案 2 :(得分:0)
Android不会在pre -ics版本上提供此行为,因此必须编写额外的代码才能完成此操作。