使用对话框android时长按菜单不起作用
Posted
技术标签:
【中文标题】使用对话框android时长按菜单不起作用【英文标题】:long press menu not working when using dialog android 【发布时间】:2015-03-09 16:54:43 【问题描述】:当对话框打开时,我在我的活动中使用长按菜单。 当我点击菜单选项时,它显示得很好,但什么也不做。
我想在单击“关闭”菜单选项时关闭对话框
请对此提出更好的建议, 这是我的代码
ImageView img,image,image1,image2;
Bitmap a;
String f,s;
SharedPreferences pref;
Editor editor;
private static int RESULT_LOAD_IMG = 3;
String imgDecodableString;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
editor = pref.edit();
img = (ImageView)findViewById(R.id.imageView1);
Button getSignature = (Button) findViewById(R.id.getSign);
getSignature.setOnClickListener(new View.OnClickListener()
public void onClick(View view)
makdrtd();
);
protected void makdrtd()
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.signdialog);
dialog.setTitle("Pen Size");
Button make = (Button) dialog.findViewById(R.id.makesign1);
image = (ImageView) dialog.findViewById(R.id.saveimg);
image1 = (ImageView) dialog.findViewById(R.id.saveimg1);
registerForContextMenu(image);
dialog.show();
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "Close");//groupId, itemId, order, title
menu.add(0, v.getId(), 0, "SMS");
@Override
public boolean onContextItemSelected(MenuItem item)
if(item.getTitle()=="Close")
image.setImageBitmap(null);
else if(item.getTitle()=="SMS")
Toast.makeText(getApplicationContext(),"sending sms code",Toast.LENGTH_LONG).show();
else
return false;
return true;
public void function1(int id)
Toast.makeText(this, "function 1 called", Toast.LENGTH_SHORT).show();
public void function2(int id)
Intent i = new Intent(MainActivity.this, SWork.class);
startActivity(i);
【问题讨论】:
看起来你只需要给 make 按钮一个点击监听器 【参考方案1】:protected void makdrtd()
Context context = this.getApplicationContext();
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.signdialog);
dialog.setTitle("Pen Size");
ImageView image = (ImageView) dialog.findViewById(R.id.saveimg);
ImageView image1 = (ImageView) dialog.findViewById(R.id.saveimg1);
Button make = (Button) dialog.findViewById(R.id.makesign1);
registerForContextMenu(image);
dialog.show();
image1.setOnLongClickListener(new OnLongClickListener()
@Override
public boolean onLongClick(View v)
AlertDialog.Builder ADB = new AlertDialog.Builder(context);
ADB.setTitle("Title of second dialog");
ADB.setMessage("Message to show user.");
ADB.setNeutralButton("ButtonText", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
if (dialog != null)
dialog.dismiss();
);
AlertDialog dialog2 = ADB.show();
return true;
);
【讨论】:
我不会在单击按钮时关闭对话框。当图像按钮长按并选择删除选项时,我想要它 所以你想在长按时显示一个对话框并给出一个删除选项,也许还有一个取消选项? 如果你想长按另一个选项,那将是 2 个对话框 它不起作用。当我长按 image1 时,它会关闭我的应用程序 应用程序是崩溃了还是刚刚关闭?以上是关于使用对话框android时长按菜单不起作用的主要内容,如果未能解决你的问题,请参考以下文章