Giter Site home page Giter Site logo

lambdatest's Introduction

#Android中如何使用Lambda表达式高效开发

当我们在Android开发中,使用Android Studio时,去写一个线程或者button的点击事件时,写完之后使用代码折叠功能,可以看到如下预览: 折叠代码 这个其实就是Lambda表达式的写法。我们发现,对于我们开发来说,不会去关心这个类以及方法,只会关心这个方法内的代码,所以其他的多余代码就显得多余了。那么,到底具体如何使用呢?

#一.在build.gradle中加入如下配置

 jackOptions{
            enabled true
        }
compileOptions{
   sourceCompatibility JavaVersion.VERSION_1_8
   targetCompatibility JavaVersion.VERSION_1_8
 }

折叠代码

#二.使用lambda表达式的三种写法

#####1.第一种方式,无参数+语句(代码块):适用于匿名内部类中方法无参数的情况

    /**
     * 第一种方式,无参数+语句(代码块):适用于匿名内部类中方法无参数的情况
     * () -> 语句
     * 或
     * () -> {代码块}
     **/
    private void threadTest() {
        /**普通写法**/
        new Thread(new Runnable() {
            @Override
            public void run() {

            }
        }).start();
        /**使用lambda表达式写法**/
        new Thread(() -> Log.d(TAG, "this is a lambda Thread")).start();
    }

#####2.第二种方式,有参数+语句:适用于匿名内部类中方法只有一个参数的情况

    /**
     * 第二种方式,有参数+语句:适用于匿名内部类中方法只有一个参数的情况
     * 方法参数 -> 语句
     * 或
     * 方法参数 -> {代码块}
     */
    private void setOnClick() {
        /**普通写法**/
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "this is a general onClick");
            }
        });
        /**使用lambda表达式写法**/
        findViewById(R.id.button).setOnClickListener(v -> Log.d(TAG, "this is a lambda onClick"));
    }

#####2.第二种方式,有参数+语句:适用于匿名内部类中方法只有一个参数的情况

    /**
     * 第三种方式,有参数+代码块:适用于匿名内部类中方法不止一个参数的情况
     * (参数1, 参数2) -> 语句
     * 或
     * (参数1, 参数2) -> {代码块}
     */
    private void setOnChecked() {
        CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
        /**普通写法**/
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.d(TAG, "this is a general onCheckedChanged");
            }
        });
        /**使用lambda表达式写法**/
        checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            Log.d(TAG, "this is a lambda onCheckedChanged");
            Log.d(TAG, "this is a lambda onCheckedChanged_isChecked=" + isChecked);
        });
    }

#GitHub

lambdatest's People

Contributors

linglongxin24 avatar

Watchers

James Cloos avatar Charlie avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.