问题
In my app, I'm using an AlertDialog with a custom view. This view contains a couple of EditTexts and a few other things. There are some listeners attached to the EditTexts that perform whatever functionality is needed.
On the button click of the dialog, I am performing some validation on the data entered into the EditTexts and display another AlertDialog if the data is invalid. Once that (second) dialog is closed, I want to remain on the main dialog (with the custom view), but for some reason that dialog is closed before my second dialog is shown. How can I keep the original (custom view) dialog still open under the second dialog?
Here's my (simplified) code:
final EntryPanel panel = new EntryPanel(OrderActivity.this);
AlertDialog dlg = new AlertDialog.Builder(OrderActivity.this)
.setCancelable(true)
.setView(panel)
.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final int id = pePanel.getProductId();
if(!isValidProduct(id)) {
new AlertDialog.Builder(OrderActivity.this)
.setMessage(R.string.error_unknown_product)
.setCancelable(true)
.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dlg2, int which) {
dlg2.cancel();
}
})
.show();
}
else {
processProductEntry(id);
dialog.dismiss();
}
}
})
.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
回答1:
So, it's not possible to keep both dialogs open. Therefore I changed the first dialog (with the custom view) to be an activity with theme being Theme.Dialog - it displays the way it should - and then open the second dialog from it. This way, when the second dialog is opened, the original activity (which looks like a dialog) is still visible in the background.
回答2:
I had a similar problem and I soled it like that:
public class PayDebtsDialogFragment extends DialogFragment implements OnClickListener
{
public interface OnDebtPayListener{
void debtPayed(BalanceItem debt, double payedAmount);
void badPayment(int errorCode);
}
private static final String LOG_TAG = "PayDebtsDialogFragment";
private static final int BAD_PARSING = -1;
private OnDebtPayListener onDebtPayListener;
private BalanceItem debtPayment;
private EditText etAmount;
public void setOnDebtPayListener(OnDebtPayListener onDebtPayListener) {
this.onDebtPayListener = onDebtPayListener;
}
public BalanceItem getDebtPayment() {
return debtPayment;
}
public void setDebtPayment(BalanceItem debtPayment) {
this.debtPayment = debtPayment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(getDialogLayout())
.setTitle("Pay debt")
.setNegativeButton("cancel", this)
.setPositiveButton("pay", this);
return builder.create();
}
@Override
public void onClick(DialogInterface dialog, int which)
{
if(which == DialogInterface.BUTTON_POSITIVE){
if(onDebtPayListener != null){
try {
String strPayment = etAmount.getText().toString().trim();
double payment = Double.valueOf(strPayment);
Log.d(LOG_TAG, "debtPayed()");
onDebtPayListener.debtPayed(debtPayment, payment);
} catch (Exception e) {
Log.d(LOG_TAG, "badPayment()", e);
onDebtPayListener.badPayment(BAD_PARSING);
}
}else{
Log.e(LOG_TAG, "onDebtPayListener is NULL");
}
}
dismiss();
}
private View getDialogLayout()
{
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.pay_debts_dialog_fragment, null);
TextView tvPayer = (TextView) dialogLayout.findViewById(R.id.tvPayDebts_payer);
tvPayer.setText(debtPayment.getPayer().getName() + " pays");
etAmount = (EditText) dialogLayout.findViewById(R.id.etPayDebts_Amount);
String hint = String.format(Locale.getDefault(), "Total debt: %.2f%s", debtPayment.getAmount(),
debtPayment.getCurrency());
etAmount.setHint(hint);
TextView tvReceiver = (TextView) dialogLayout.findViewById(R.id.tvPayDebts_Receiver);
tvReceiver.setText("to " + debtPayment.getReceiver().getName());
return dialogLayout;
}
}
- I used a DialogFragment and defined my own listener with 2 mothods.
- I implement that interface inside my activity.
- When the input is bad, the activity shows error dialog (another dialog), and when it close I show the first dialog again.
回答3:
I already faced the same problem and searched a lot but didn't find anything relevant to this.
So, I just used a little hack there to achieve almost my requirements.
First of I declare AlertDialogs :
AlertDialog.Builder subDialog,mainDialog;
Then use hack like this :
subDialog = new AlertDialog.Builder(Activity2.this)
.setMessage("New Dialog Opened")
.setCancelable(true)
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dlg2, int which) {
dlg2.cancel();
mainDialog.show();
}
});
mainDialog = new AlertDialog.Builder(Activity2.this)
.setCancelable(true)
.setMessage("First Dialog Opened")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
subDialog.show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
mainDialog.show();
What I did is on ok action of secondly opened subdialog, I again call show action for MainDialog.
Hope it will helps you.
Thanks.
来源:https://stackoverflow.com/questions/14566593/displaying-second-dialog-from-button-click-in-first-dialog-closes-first-dialog