问题
Hi there,
@Override
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
inviteView = getLayoutInflater().inflate(R.layout.invite_dialog, null);
builder.setView(inviteView);
sendSmsButton = (Button) inviteView.findViewById(R.id.sendSMSButton);
sendEmailButton = (Button) inviteView.findViewById(R.id.sendEmailButton);
builder.setTitle(R.string.invite_callrz_title);
sendSmsButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
sendEmailButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
builder.setNegativeButton(R.string.cancelItem,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
return builder.create();
}
I have a custom layout with 2 buttons on it.I also use a default nagativeButton to dismis the dialog. My question is I would like to dismis the dialog bar when the custom buttons are clicked. Basically, when the button is clicked, it will do some staff and dismis the dialog. which method I should call?
回答1:
try this
AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog alertDialog;
alertDialog = builder.create();
sendSmsButton = (Button) builder.findViewById(R.id.sendSMSButton);
sendSmsButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
来源:https://stackoverflow.com/questions/8091858/how-to-dismis-custom-dialog-from-a-custom-button-in-dialog