V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
codechaser
V2EX  ›  Android

Android 不支持 lambda 吗?

  •  
  •   codechaser · 2018-08-27 19:40:31 +08:00 · 5555 次点击
    这是一个创建于 2062 天前的主题,其中的信息可能已经有所发展或是发生改变。
    各位好,比如给一个按钮加监听器,直接用 lambda 就会报错?是因为监听器里面不止一个方法要重载吗?
    17 条回复    2018-08-28 16:56:57 +08:00
    tougee
        1
    tougee  
       2018-08-27 19:47:24 +08:00
    lambda 是语言特性,Android 不是一门语言
    codechaser
        2
    codechaser  
    OP
       2018-08-27 19:51:12 +08:00
    @tougee 我知道啊,JDK 用的是 1.8,但是在 AS 里用 lambda 会提示错误
    zpxshl
        3
    zpxshl  
       2018-08-27 19:53:13 +08:00 via Android
    支持,用了挺久了。
    zj299792458
        4
    zj299792458  
       2018-08-27 20:12:55 +08:00
    请将 AS 里的 java 环境切换成 8
    gam2046
        6
    gam2046  
       2018-08-27 20:17:27 +08:00
    喜欢这样的语法糖,那么就直接用 Kotlin 呗。语法很接近,上手难度很低。

    有很多糖吃哦~~
    vjnjc
        7
    vjnjc  
       2018-08-27 20:22:33 +08:00 via Android
    没有支持所有 Java8 特性,但是用 lambda 写个匿名类还是没问题的
    zjp
        8
    zjp  
       2018-08-27 20:28:17 +08:00 via Android
    需要在 gradle 配置里开启 Java8 的支持 https://developer.android.com/studio/write/java8-support?hl=zh-cn
    PhxNirvana
        9
    PhxNirvana  
       2018-08-27 23:32:54 +08:00
    是的,不支持,android runtime 在 N 还是 O 才引入 java8 的语言特性,所以除非你不要低版本的用户,就可以开启 java8
    如果想在低版本支持,可以引入 retrolambda 包来支持 lambda,
    另外我用 kotlin-。-
    Michelangelono
        10
    Michelangelono  
       2018-08-28 08:52:02 +08:00
    使用 kotlin 就行了
    Trumeet
        11
    Trumeet  
       2018-08-28 08:55:33 +08:00 via Android
    使用 Java 8
    wwqgtxx
        12
    wwqgtxx  
       2018-08-28 13:18:25 +08:00
    @PhxNirvana 你的知识库该更新了,根据 android 官方文档,lambda 特性在任意 android sdk 目标版本上均可用
    https://developer.android.com/studio/write/java8-support?hl=zh-cn
    PhxNirvana
        13
    PhxNirvana  
       2018-08-28 14:10:54 +08:00
    @wwqgtxx #12 好吧,我的锅。
    以前用 java8 的 api 报错所以认为都会出错了
    于是新问题来了,怎么在用 lambda 的时候避免使用 java8 的 api,即只使用不会出错的东西-。-
    wwqgtxx
        14
    wwqgtxx  
       2018-08-28 16:20:57 +08:00
    @PhxNirvana 一般来说只要你的 android studio 版本足够新,发现了调用当前 Min Sdk Version 不支持的 api 都会有提示呀,如果你不看选择继续使用,那就没办法了
    PhxNirvana
        15
    PhxNirvana  
       2018-08-28 16:24:15 +08:00
    @wwqgtxx #14 不不,他不会提示 java 的 api,你可以试一下在高版本使用 List.sort(),然后在低版本上运行,全程无提示,但必崩
    wwqgtxx
        16
    wwqgtxx  
       2018-08-28 16:34:43 +08:00
    @PhxNirvana 试了一下,在 AS3.1.2 上,List.sort()会提示警告的
    Call requires API level 24 (current min is 15): java.util.List#sort less... (Ctrl+F1)
    This check scans through all the Android API calls in the application and warns about any calls that are not available on all versions targeted by this application (according to its minimum SDK attribute in the manifest). If you really want to use this API and don't need to support older devices just set the minSdkVersion in your build.gradle or AndroidManifest.xml files. If your code is deliberately accessing newer APIs, and you have ensured (e.g. with conditional execution) that this code will only ever be called on a supported platform, then you can annotate your class or method with the @TargetApi annotation specifying the local minimum SDK to apply, such as @TargetApi(11), such that this check considers 11 rather than your manifest file's minimum SDK as the required API level. If you are deliberately setting android: attributes in style definitions, make sure you place this in a values-vNN folder in order to avoid running into runtime conflicts on certain devices where manufacturers have added custom attributes whose ids conflict with the new ones on later platforms. Similarly, you can use tools:targetApi="11" in an XML file to indicate that the element will only be inflated in an adequate context.
    PhxNirvana
        17
    PhxNirvana  
       2018-08-28 16:56:57 +08:00
    @wwqgtxx #16 卧槽。。。估计被修复了吧 Orz。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2598 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 04:28 · PVG 12:28 · LAX 21:28 · JFK 00:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.