Java8 lamada表达式的简单使用

本文介绍了如何在Java8中配置并使用Lambda表达式,通过示例展示了在MainActivity类中的应用,活动布局activity_main.xml中包含六个Button,用于演示Lambda的事件处理。
摘要由CSDN通过智能技术生成

首先是配置。。。
在project的build.gradle中添加

classpath 'me.tatarka:gradle-retrolambda:3.2.0'

这里写图片描述

在module的build.gradle中添加

 //设置JDK1.8
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    jackOptions {
            enabled true
        }

位置如下:

这里写图片描述

哈哈哈,接下来就开始我们的lamda之旅啦。。。

MainActivity.class

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "cwy";
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button1).setOnClickListener(view -> onButton1());
        findViewById(R.id.button2).setOnClickListener(view -> onButton2());
        findViewById(R.id.button3).setOnClickListener(view -> onButton3());
        findViewById(R.id.button4).setOnClickListener(view -> onButton4());
        findViewById(R.id.button5).setOnClickListener(view -> onButton5());
        findViewById(R
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值