gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:41:29 25 4
gpt4 key购买 nike

只是想知道在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com