Android N中使用Java8

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013624138/article/details/51917336

今天的Google I/O大会上,发布的Android Studio 2.2,增强了对Java8语言的支持。这几天体验了一下,在此做个记录。
实现了一个Snackbar的Demo,是 Android Support Library 22.2.0 里面新增提供的一个控件,是Toast的加强版。
使用时添加compile语句

compile 'com.android.support:design:24.0.0'

然后配置Java8

  1. 配置jdk路径:
    配置jdk路径

  2. 配置app/build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"
    defaultConfig {
        applicationId "com.mark.snackbardemo"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        jackOptions {
            enabled true
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha1'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:design:24.0.0'
}

其中
jack配置

Java8编译
设置使用Java 8和Jack编译

接下来,就可以使用Java8写代码了

package com.mark.snackbardemo;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button snackbar = (Button) findViewById(R.id.button);
        snackbar.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Snackbar.make(view, "This is a Snackbar !", Snackbar.LENGTH_SHORT)
                .setAction("COOL", view1 ->
                        Toast.makeText(this, "This is a Toast !", Toast.LENGTH_SHORT).show()
                )
                .show();
    }
}

其中
lambda表达式
使用Lambda表达式,代替了View.OnClickListener接口的匿名实现类

运行效果:
Snackbar

上面也可以使用两次Lambda表达式:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button snackbar = (Button) findViewById(R.id.button);
        snackbar.setOnClickListener(view ->
                Snackbar.make(view, "This is a Snackbar !", Snackbar.LENGTH_SHORT)
                        .setAction("COOL", view1 ->
                                Toast.makeText(this, "This is a Toast !", Toast.LENGTH_SHORT).show()
                        )
                        .show());
    }

}

项目源码:https://github.com/Freakmaster/SnackbarDemo

没有更多推荐了,返回首页