Android display another dialog from a dialog

2019-01-14 12:49发布

I am trying to display a dialog from the onClick listener of a button of another dialog, but the 2nd dialog won't display. I searched and found a similar problem- Dialogs order in Android, tried the solution provided, but even that does not work.

My code is very similar to the one provided in the answer.

public void onClick(DialogInterface dialog, int id) { showDialog(SECOND_DIALOG); dialog.dismiss(); }

any help will be really appreciated.

Thanks,

Akshay

4条回答
forever°为你锁心
2楼-- · 2019-01-14 13:16

I know this was asked a while ago but here's a pretty neat solution I found.

I define an interface like this:

public interface OpenDialog {

public void showDialog(DialogFragment dialog);

}

Which my activity then implements, passing a reference to itself to the dialog when it's opened, using my InterfaceHolder class:

public class MyActivity extends FragmentActivity implements OpenDialog {

@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);

    InterfaceHolder.set(this);        
    showDialog(new DialogOne());

}

public void showDialog(DialogFragment dialog) {
    dialog.show(getSupportFragmentManager(), "tag");
}

InterfaceHolder is just a class with a static reference to the interface I use to pass it around:

public class InterfaceHolder {
private static OpenDialog openDialog;

public void set(OpenDialog openDialog)
    this.openDialog = openDialog;
}

public void get()
    return openDialog;
}

So the showDialog method will display any dialog that I pass into it; as you can see, I do this to display DialogOne. Now, if I want to open a new dialog called "DialogTwo" inside "DialogOne" I can call it by writing:

InterfaceHolder.get().showDialog(new DialogTwo());
dismiss();

And voila, DialogTwo is shown. Obviously, you have to be careful to ensure that a reference to your activity has been passed to the InterfaceHolder (a nice way to do this is to put InterfaceHolder.set(this); inside the showDialog method), but otherwise this seems to work beautifully.

查看更多
孤傲高冷的网名
3楼-- · 2019-01-14 13:17

my suggestion is this code. dialog 1 has a button when click it then dialog 2 will call.

    private Dialog dialog;
    private AlertDialog.Builder voteBuilder;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);     

    // Firat dialog
            dialog = new Dialog(QrCodeReader.this);
            dialog.setContentView(R.layout.customdialog_qr);
            dialog.setCancelable(false);

    // Second dialog
            showVotingDialog();

    //set up button of dialog 1
            Button btnVote = (Button) dialog.findViewById(R.id.btnVote);
            btnVote.setOnClickListener(new OnClickListener() {
            @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    voteBuilder.show();
                }
            });

dialog.show();
    }

    private void showVotingDialog() {
            voteBuilder = new AlertDialog.Builder(this);
            voteBuilder.setTitle(R.string.votingdialog_title);
            voteBuilder.setCancelable(false);
            LayoutInflater inflater = getLayoutInflater();
            final View checkboxLayout = inflater.inflate(R.layout.voting_dialog, null);
            voteBuilder.setView(checkboxLayout);
            voteBuilder.setPositiveButton(R.string.menu_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    RadioGroup radioGroup = (RadioGroup) checkboxLayout.findViewById(R.id.vote_radiogroup);
                    int checkedRadioButton = radioGroup.getCheckedRadioButtonId();
                    int radioButtonSelected = 0;
                    switch (checkedRadioButton) {
                        case R.id.vote_item1 : radioButtonSelected = 1; break;
                        case R.id.vote_item2 : radioButtonSelected = 3; break;
                        case R.id.vote_item3 : radioButtonSelected = 5; break;
                        case R.id.vote_item4 : radioButtonSelected = 10; break;
                        case R.id.vote_item5 : radioButtonSelected = 20; break;
                        default: radioButtonSelected = 0;
                    }

                    sendVoteBySMS(radioButtonSelected);
                }
            });
            voteBuilder.setNegativeButton(R.string.menu_cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    QrCodeReader.this.finish();
                }
            });

            voteBuilder.create();
    //      voteBuilder.show();
        }
查看更多
三岁会撩人
4楼-- · 2019-01-14 13:21

Just Hide and Show Views On Dialog Clicks

查看更多
放我归山
5楼-- · 2019-01-14 13:43

This is how I'm doing it:

    if (!appPrefs.getAcceptedUsageAggrement()) {
        tracker.trackPageView("/UsageAgreementDialog");
        acceptedUsage_alertDialog = new AlertDialog.Builder(BroadcastSMSActivity.this)
        .setTitle(R.string.accept_usage_title)
        .setMessage(R.string.accept_usage_message)
        .setNegativeButton(android.R.string.cancel, new AlertDialog.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        })
        .setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                if (appPrefs.getAppVer().equals("")) {
                    tracker.trackEvent("Application", "Install", getAppVerName(), 1);
                } else {
                    tracker.trackEvent("Application", "Upgrade", appPrefs.getAppVer().toString()+"->"+getAppVerName(), 1);
                }
                displayRecentChanges = true;
                appPrefs.saveAppVer(getAppVerName());
                appPrefs.saveAcceptedUsageAggrement(true);
            // Display Recent Changes on 1st use of new version
                if (displayRecentChanges) {
                    tracker.trackPageView("/RecentChangesDialog");
                    recentChanges_alertDialog = new AlertDialog.Builder(BroadcastSMSActivity.this)
                    .setTitle(getString(R.string.changes_title, getAppVerName()))
                    .setMessage(R.string.changes_dialog)
                    .setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            recentChanges_alertDialog.cancel();
                            acceptedUsage_alertDialog.cancel();
                        }
                    })
                    .create();
                    recentChanges_alertDialog.show();
                }
            }
        })
        .create();
        acceptedUsage_alertDialog.show();
    }
查看更多
登录 后发表回答