当对话框打开时,我在活动中使用长按菜单。 当我点击菜单选项时,它显示效果不好但没有做任何事情。
我想在点击“关闭”菜单选项时关闭对话框
请对此提出更好的建议, 这是我的代码
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);
}
}
答案 0 :(得分:0)
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;
}
});
}