开发者

Android display another dialog from a dialog

I am trying to display a dialog from the onClick listener of a button of another dialog, but 开发者_如何学Pythonthe 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


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();
    }


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.


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();
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜