我可以使用以下方法禁用上下文菜单中的项目:
menu.getItem(1).setEnabled(false);
在 onCreateContextMenu 中,但是如果我以后想在同一个活动中将其设置为 true 怎么办?我怎么能在运行时做到这一点?
编辑:
// Create context menu
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Options for this image"); // Title for the menu
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_1_menu, menu);
menu.getItem(1).setEnabled(false);
}
这是我要更改要启用的项目的地方:
// Handle return values from different activities
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 1) {
// Get value from intent
String imageId = data.getExtras().getString("imageId");
// Show full screen image
Bitmap bitmap = BitmapFactory.decodeFile(imageId);
imageView.setImageBitmap(bitmap);
}
else if(requestCode == 2) {
String contact1 = data.getExtras().getString("contactName");
String contact2 = data.getExtras().getString("test");
Toast.makeText(Activity_1.this, "" + "Value from activity 3: " + contact1 + " " + contact2, Toast.LENGTH_SHORT).show();
CHANGE MENU ITEM 1 TO ENABLE HERE!!!!
}
}
}
}