java - 具有可选参数的 Android 警报监听器

标签 java android android-alertdialog

只是想知道在 Android/Java 中是否可以执行以下操作。 在我的一项 Activity 中,我有许多警报对话框,按下按钮即可启动新 Intent 。我不想做很多重复,而是想尝试构建如下所示的东西:

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()对我来说似乎不正确。是否可以这样处理我的问题。也许有人有一个(更简单的)替代方案?

编辑1:替代代码:

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

关于java - 具有可选参数的 Android 警报监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48517753/

相关文章:

java - 图形 (java.awt.Graphics) 无法在调用时工作

java 替换列表内容或替换引用值本身

OutputStream 上的 Java 自动 HTTP header

android - 从手机获取联系人需要太多时间

android - 如何在正在运行的线程中显示警报对话框?

java - 以最快的方式找到所有可能的子字符串

android - WhatsApp拦截个人资料照片上传

java - 如何在android中分离通过蓝牙接收的数据

android - 显示 AlertDialog 使我的应用程序崩溃。

android - 处理 AlertDialog.Builder 中的后退按钮