如何使用 Android 正确编码确认对话框?

How to properly code the confirm dialogs with Android?

这应该是一件很常见的事情:有对话框来确认继续与用户的交互流程。但是我能想到的最好的信息对我来说似乎并不好。我主要扩展了 DialogFragment(在第一次搜索示例之后)并实现了 NoticeDialogListener。

我开始相信这不是更好的方法,因为就程序流程而言,它非常麻烦。程序流程方面我知道它可能很麻烦,因为 Dialog 从主线程开始显示为另一个线程,但我想应该有更好的方法将不同的响应方法分配给不同的对话框。但是除了接下来的内容,我还没有找到其他方法。

希望我已经清楚地描述了我的问题。预先感谢您的回复。

public class Confirm extends DialogFragment {

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(sQ);
        builder
                .setPositiveButton(sYes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        mListener.Yes();
                    }
                })
                .setNegativeButton(sNo, null);
        return builder.create();
    }

    public interface NoticeDialogListener {
        void Yes();
    }
    private NoticeDialogListener mListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mListener = (NoticeDialogListener) activity;
    }
}

public class Main extends ActionBarActivity
        implements Confirm.NoticeDialogListener {
...

    private int iDialogMode;
    private final static int DIALOG_ST_0 = 0;
    private final static int DIALOG_ST_1 = DIALOG_ST_1 + 1;
    private final static int DIALOG_ST_2 = DIALOG_ST_1 + 1;

    @Override
    public void Yes() {
        switch (iDialogMode) {
            case DIALOG_ST_0: // follow up HERE0 for what that dialog prompted
                break;
            case DIALOG_ST_1: // HERE1: feeling not smart
                break;
            case DIALOG_ST_2: // HERE2: believe there should be a better way
                break;
        }
    }

    public ... State_0_doing_something (...) {
        ...
        Confirm diaBox = new Confirm (...);
        iDialogMode = DIALOG_ST_0;
        diaBox.show(getSupportFragmentManager(), "State_0");
        // what's supposed to continue if confirmed will be followed up in HERE0 in Yes() 
    }

    public ... State_1_doing_something_else (...) {
        ...
        Confirm diaBox = new Confirm (...);
        iDialogMode = DIALOG_ST_1;
        diaBox.show(getSupportFragmentManager(), "State_2");
        // what's supposed to continue if confirmed will be followed up in HERE1 in Yes() 
    }

    public ... State_2_doing_yet_something_else (...) {
        ...
        Confirm diaBox = new Confirm (...);
        iDialogMode = DIALOG_ST_2;
        diaBox.show(getSupportFragmentManager(), "State_3");
        // what's supposed to continue if confirmed will be followed up in HERE2 in Yes() 
    }
}

我在考虑是否可以将不同的点击侦听器附加到创建的每个确认对话框,而不是使用 global variable/member 设置对话框 mode/state那。这里缺少函数指针...

"By the way, how come I couldn't properly post the beginning declaration and ending bracket in the grey box? "

在该文本前添加 4 个空格,或在代码文本的开头和结尾添加 ` 字符。 例子: Fake code that does nothing

"Program flow-wise I understand it can be cumbersome anyway as Dialog appears as another thread from the main to begin with"

这基本上就是为什么它不打算让用户继续使用对话框的原因。到目前为止,我看到大多数程序的方式是,只有可能严重延迟应用程序或可能向用户收费的操作才会使用对话框。

此外,请注意 Activity 生命周期会让您记住它的 "state",这会进一步增加您的应用 onPause/onResume 的问题。

已解决:唯一的问题是没有那么好看

AlertDialog.Builder builder = new AlertDialog.Builder(Home.theContext);
            builder.setMessage(R.string.confirm_delete);
            builder.setCancelable(true);
            builder.setPositiveButton(R.string.confirm_yes, vCabDelete());
            builder.setNegativeButton(Home.theContext.getString(R.string.confirm_no), null);
            AlertDialog dialog = builder.create();
            dialog.show();

....

private static DialogInterface.OnClickListener vCabDelete() {
    return new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface di, int id) {
            ....
        }
    };
}