3 ms·
Great getting started page. Short and direct. As a Kotlin noob, can someone explain to me why the Kotlin on-click listener doesn't need to specify onClick() li
by pawadu 9y ago
Great getting started page. Short and direct.
As a Kotlin noob, can someone explain to me why the Kotlin on-click listener doesn't need to specify onClick() like the Java one does?
- realharo 9y agohttps://kotlinlang.org/docs/reference/java-interop.html#sam-conversions https://kotlinlang.org/docs/reference/java-interop.html#sam-...
- htgb 9y agoActually, this is similar to what you can do in Java by using lambdas (Retrolambda for pre-Java 8, e.g. Android pre-SDK 24). Excerpt from a blog post [1] about it: mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // do something here } }); can be replaced with mButton.setOnClickListener((View v) -> { // do something here }); which is nice for all callbacks, but especially together with ReactiveX where they are abundant. [1] http://zserge.com/blog/android-lambda.html http://zserge.com/blog/android-lambda.html
- tjohns 9y agoWorth noting for completeness... In Kotlin you can take that a step further: mButton.setOnClickListener { /* do something here */ } (In addition to SAM conversions, both the parenthesis and input variable are optional.)