是的。您可以使用。考虑这个例子......
final DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AlertDialog alertDialog = (AlertDialog) dialog;
String s = (String) alertDialog.getButton(
DialogInterface.BUTTON_POSITIVE).getTag();
boolean isDialog1 = s.equals("DIALOG1");
switch (which) {
case DialogInterface.BUTTON_NEGATIVE:
if(isDialog1) {
Toast.makeText(getApplicationContext(), "Negative in Dialog1", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Negative in Dialog2", Toast.LENGTH_SHORT).show();
}
break;
case DialogInterface.BUTTON_NEUTRAL:
if(isDialog1) {
Toast.makeText(getApplicationContext(), "Neutral in Dialog1", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Neutral in Dialog2", Toast.LENGTH_SHORT).show();
}
break;
case DialogInterface.BUTTON_POSITIVE:
if(isDialog1) {
Toast.makeText(getApplicationContext(), "Positive in Dialog1", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Positive in Dialog2", Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
};
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(
SecondActivity.this);
builder.setTitle("Dialog1 Title");
builder.setMessage("Dialog1 Message");
builder.setPositiveButton("Yes", clickListener);
builder.setNegativeButton("No", clickListener);
builder.setNeutralButton("Cancel", clickListener);
AlertDialog alertDialog = builder.create();
alertDialog.show();
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setTag(
"DIALOG1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(
SecondActivity.this);
builder.setTitle("Dialog2 Title");
builder.setMessage("Dialog2 Message");
builder.setPositiveButton("Yes", clickListener);
builder.setNegativeButton("No", clickListener);
builder.setNeutralButton("Cancel", clickListener);
AlertDialog alertDialog = builder.create();
alertDialog.show();
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setTag(
"DIALOG2");
}
});