我的 Android 工作室设置了这些:
classpath "me.tatarka:gradle-retrolambda:3.2.2"
classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'
我正在尝试使用 lambda 来了解我能做什么或不能做什么。
当我执行以下代码时:
alertDialogBuilder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
IDE 使 new DialogInterface.OnClickListener()
变灰,告诉我它可以用 lambda 代替。没有更多或更少。查后several examples .我试过这样的事情:
alertDialogBuilder.setPositiveButton("Okay", (DialogInterface dialog) -> {
dialog.cancel();
});
还有这些:
alertDialogBuilder.setNegativeButton((DialogInterface) d -> d.cancel());
错误包括:
Error:(99, 64) error: incompatible types: DialogInterface is not a functional interface multiple non-overriding abstract methods found in interface DialogInterface
在这种情况下我应该如何使用 lambda?
最佳答案
onClick
有两个参数,而您的示例只尝试显示一个。请务必在您的 lambda 中包含 which
参数。
new AlertDialog.Builder(this).setPositiveButton("Okay",
(dialog, which) -> dialog.cancel());
关于android - 使用 lambda 表达式创建一个 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32623026/