繁体   English   中英

带有可选参数的Android Alertlistener

[英]Android alertlistener with optional parameters

只是想知道是否可以在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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM