Android 不支持 lambda 吗?

2018-08-27 19:40:31 +08:00
 codechaser
各位好,比如给一个按钮加监听器,直接用 lambda 就会报错?是因为监听器里面不止一个方法要重载吗?
5812 次点击
所在节点    Android
17 条回复
tougee
2018-08-27 19:47:24 +08:00
lambda 是语言特性,Android 不是一门语言
codechaser
2018-08-27 19:51:12 +08:00
@tougee 我知道啊,JDK 用的是 1.8,但是在 AS 里用 lambda 会提示错误
zpxshl
2018-08-27 19:53:13 +08:00
支持,用了挺久了。
zj299792458
2018-08-27 20:12:55 +08:00
请将 AS 里的 java 环境切换成 8
yukiww233
2018-08-27 20:16:23 +08:00
gam2046
2018-08-27 20:17:27 +08:00
喜欢这样的语法糖,那么就直接用 Kotlin 呗。语法很接近,上手难度很低。

有很多糖吃哦~~
vjnjc
2018-08-27 20:22:33 +08:00
没有支持所有 Java8 特性,但是用 lambda 写个匿名类还是没问题的
zjp
2018-08-27 20:28:17 +08:00
需要在 gradle 配置里开启 Java8 的支持 https://developer.android.com/studio/write/java8-support?hl=zh-cn
PhxNirvana
2018-08-27 23:32:54 +08:00
是的,不支持,android runtime 在 N 还是 O 才引入 java8 的语言特性,所以除非你不要低版本的用户,就可以开启 java8
如果想在低版本支持,可以引入 retrolambda 包来支持 lambda,
另外我用 kotlin-。-
Michelangelono
2018-08-28 08:52:02 +08:00
使用 kotlin 就行了
Trumeet
2018-08-28 08:55:33 +08:00
使用 Java 8
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
2018-08-28 14:10:54 +08:00
@wwqgtxx #12 好吧,我的锅。
以前用 java8 的 api 报错所以认为都会出错了
于是新问题来了,怎么在用 lambda 的时候避免使用 java8 的 api,即只使用不会出错的东西-。-
wwqgtxx
2018-08-28 16:20:57 +08:00
@PhxNirvana 一般来说只要你的 android studio 版本足够新,发现了调用当前 Min Sdk Version 不支持的 api 都会有提示呀,如果你不看选择继续使用,那就没办法了
PhxNirvana
2018-08-28 16:24:15 +08:00
@wwqgtxx #14 不不,他不会提示 java 的 api,你可以试一下在高版本使用 List.sort(),然后在低版本上运行,全程无提示,但必崩
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
2018-08-28 16:56:57 +08:00
@wwqgtxx #16 卧槽。。。估计被修复了吧 Orz。。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/483753

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX