只是想知道是否可以在Android / Java中执行以下操作。
在我的一项活动中,我有许多“警报对话框”,只需按一下按钮即可启动新的意图。我不想做很多重复,而是尝试构建如下内容:

Alert(this, "Do you like A?", "yes", "no, I like B", classA.class, classB.class)

public void Alert(Context context, String question, String answerPositive, String answerNegative, Class newIntentA, Class newIntentB) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);
    alert.setMessage(question).setPositiveButton(answerPositive, AlertListener(context, newIntentA, newIntentB)).setNegativeButton(answerNegative, AlertListener(context, newIntentA, newIntentB)).show();
}

DialogInterface.OnClickListener AlertListener(Context context, Class newIntentA, Class newIntentB) = new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case DialogInterface.BUTTON_POSITIVE:
                Intent a = new Intent(context, newIntentA);
                startActivity(a);
                break;

            case DialogInterface.BUTTON_NEGATIVE:
                Intent b = new Intent(context, newIntentB);
                startActivity(b);
                break;
        }
    }
};


这有可能吗?现在,我在打电话给AlertListener(context, newIntentA, newIntentB)时搞砸了。而且DialogInterface.OnClickListener AlertListener(Context context, Class newIntentA, Class newIntentB) = new DialogInterface.OnClickListener()在我看来似乎不正确。是否有可能像这样解决我的问题。也许有人有(更轻松)的选择?

EDIT1:替代代码:

public void Alert(final Context context, String question, String answerPositive, String answerNegative, final Class newIntentA, final Class newIntentB) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);
    alert.setMessage(question).setPositiveButton(answerPositive, AlertListener).setNegativeButton(answerNegative, AlertListener).show();

    DialogInterface.OnClickListener AlertListener = new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case DialogInterface.BUTTON_POSITIVE:
                    Intent a = new Intent(context, newIntentA);
                    startActivity(a);
                    break;

                case DialogInterface.BUTTON_NEGATIVE:
                    Intent b = new Intent(context, newIntentB);
                    startActivity(b);
                    break;
            }
        }
    };
}


现在的问题是,我得到了“无法解析符号AlertListener”。

最佳答案

解决了:

Alert(this, "do you want A?", "yes", "no I want B", A.class, B.class);
//or
Alert(this, "do you want A?", "yes", "no I want nothing", A.class, null);

public void Alert(final Context context, String question, String answerPositive, String answerNegative, final Class newIntentA, final Class newIntentB) {
    DialogInterface.OnClickListener AlertListener = new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case DialogInterface.BUTTON_POSITIVE:
                    if (newIntentA != null) {
                        Intent a = new Intent(context, newIntentA);
                        startActivity(a);
                    }
                    break;

                case DialogInterface.BUTTON_NEGATIVE:
                    if (newIntentB != null) {
                        Intent b = new Intent(context, newIntentB);
                        startActivity(b);
                    }
                    break;
            }
        }
    };

    AlertDialog.Builder alert = new AlertDialog.Builder(context);
    alert.setMessage(question).setPositiveButton(answerPositive, AlertListener).setNegativeButton(answerNegative, AlertListener).show();
}

08-17 23:40