JDK 8 lambda expressions and examples of use in Android Studio

Source: Internet
Author: User

One feature that used to be interesting to Java is that it supports anonymous internal classes, and recently discovered that JDK8 has supported lambda and has a simpler way to implement anonymous internal classes. This will make the programmer more comfortable and prefer Java. Many years ago I think Java Grammar and C # syntax is a bit far behind, without C # so easy to write, now feel jdk8 is also very good. To do the technology or to look forward.

Final TextView txtView2 = (TextView)this = (Button) Findviewbyid (R.id.btnchangetext); Btnchangeview.setonclicklistener (new  View.onclicklistener () {    @Override      publicvoid  OnClick (View v) {        txtview2.settext ("Text changed");    }});

The new implementation method:

Final TextView txtView2 = (TextView)this = (Button) Findviewbyid (r.id.btnchangetext); // Btnchangeview.setonclicklistener (E-log.i ("MyTag", "button Clicked.")); assert NULL   {    "Btnchangeview Clicked:" + View.getid ());    Txtview2.settext (new  Date (). toString ());});

Reference:

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

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

Configuration needs to be changed: Build.gradle

defaultConfig {  ... jackOptions { enabled true } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }
Apply plugin: ' Com.android.application 'Android {compilesdkversion23buildtoolsversion"24.0.2"Defaultconfig {ApplicationID"Com.firstapp.test.firstapp"minsdkversion16targetsdkversion23Versioncode1Versionname"1.0"jackoptions {enabledtrue}} buildtypes {release {minifyenabledfalseproguardfiles Getdefaultproguardfile (' Proguard-android.txt '), ' Proguard-rules.pro '}} compileoptions {sourcecompatibility1.8targetcompatibility1.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 expressions and examples of use in Android Studio

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.