Android在对话框中显示另一个对话框

时间:2011-04-14 11:29:40

标签: android dialog

我正在尝试从另一个对话框的按钮的onClick侦听器显示一个对话框,但第二个对话框将不会显示。我搜索并发现了类似的问题 - Dialogs order in Android,尝试了所提供的解决方案,但即使这样也行不通。

我的代码与答案中提供的代码非常相似。

  

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

任何帮助都会非常感激。

谢谢,

阿克沙伊

3 个答案:

答案 0 :(得分:8)

我正是这样做的:

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

答案 1 :(得分:5)

我知道这是前一段时间被问过的,但我发现这是一个非常简洁的解决方案。

我定义了这样的界面:

public interface OpenDialog {

public void showDialog(DialogFragment dialog);

}

然后我的活动实现了,使用我的InterfaceHolder类将对它自己的引用传递给对话框:

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只是一个类,它带有我用来传递它的接口的静态引用:

public class InterfaceHolder {
private static OpenDialog openDialog;

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

public void get()
    return openDialog;
}

因此showDialog方法将显示我传递给它的任何对话框;如你所见,我这样做是为了显示DialogOne。现在,如果我想在“DialogOne”中打开一个名为“DialogTwo”的新对话框,我可以通过以下方式调用它:

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

瞧,DialogTwo正在展示。显然,您必须小心确保已将对您的活动的引用传递给InterfaceHolder(一种很好的方法是将InterfaceHolder.set(this)放在showDialog方法中),但是否则这似乎有效精美。

答案 2 :(得分:2)

我的建议是这段代码。对话框1在单击时有一个按钮,然后对话框2将调用。

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