jdk 8 lambda運算式 及在Android Studio的使用樣本

來源:互聯網
上載者:User

標籤:

以前覺得java讓人覺得有趣的一個特點是支援:匿名內部類,而最近發現jdk8已支援lambda並有更簡便的方式,來實現匿名內部類. 這會讓程式員更舒服,更喜歡java. 多年前覺得java文法和C#文法差得有點遠,沒有C#那麼寫來方便,現在覺得jdk8也很不錯了。做技術還是要向前看。

final TextView txtView2 = (TextView)this.findViewById(R.id.txtView2);Button btnChangeView = (Button)findViewById(R.id.btnChangeText);btnChangeView.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {        txtView2.setText("Text 改變");    }});

新的實現方式:

final TextView txtView2 = (TextView)this.findViewById(R.id.txtView2);Button btnChangeView = (Button)findViewById(R.id.btnChangeText);//btnChangeView.setOnClickListener(e -> Log.i("MyTag", "Button Clicked.") );assert btnChangeView != null;btnChangeView.setOnClickListener((view) -> {    Log.i(TAG, "btnChangeView Clicked:" + view.getId());    txtView2.setText("Text 改變:" + new Date().toString());});

 

參考:

http://stackoverflow.com/questions/37004069/errorjack-is-required-to-support-java-8-language-features

http://www.jianshu.com/p/5fc2b3362702

需要更改配置: build.gradle

defaultConfig {  ... jackOptions { enabled true    }  } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }
apply plugin: ‘com.android.application‘android {    compileSdkVersion 23    buildToolsVersion "24.0.2"    defaultConfig {        applicationId "com.firstapp.test.firstapp"        minSdkVersion 16        targetSdkVersion 23        versionCode 1        versionName "1.0"        jackOptions {            enabled true        }    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘        }    }    compileOptions {        sourceCompatibility 1.8        targetCompatibility 1.8    }}dependencies {    compile fileTree(include: [‘*.jar‘], dir: ‘libs‘)    testCompile ‘junit:junit:4.12‘    compile ‘com.android.support:appcompat-v7:23.4.0‘}

 

jdk 8 lambda運算式 及在Android Studio的使用樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.