[英]Use lambda expression to create an AlertDialog
我的Android工作室设置如下:
classpath "me.tatarka:gradle-retrolambda:3.2.2"
classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'
我正在尝试使用lambdas知道我能做什么或不做什么。
当我做以下代码时:
alertDialogBuilder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
IDE灰显了new DialogInterface.OnClickListener()
告诉我它可以用lambda替换。 没有更多或更少。 看了几个 例子 。 我尝试过这样的事情:
alertDialogBuilder.setPositiveButton("Okay", (DialogInterface dialog) -> {
dialog.cancel();
});
还有这些:
alertDialogBuilder.setNegativeButton((DialogInterface) d -> d.cancel());
其中的错误:
错误:(99,64)错误:不兼容的类型:DialogInterface不是功能接口在接口DialogInterface中找到的多个非重写抽象方法
在这种情况下我应该如何使用lambda?
onClick
有两个参数,你的示例尝试只显示一个。 请务必在lambda中包含which
参数。
new AlertDialog.Builder(this).setPositiveButton("Okay",
(dialog, which) -> dialog.cancel());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.