在 Android 中结合 Java 和 Kotlin [英] Combine Java with Kotlin in Android

查看:35
本文介绍了在 Android 中结合 Java 和 Kotlin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Android 应用程序开发方面从 Java 稍微转移到 Kotlin,但是在某些情况下我不想在 Kotlin 中编码并且想要那些特殊情况是用 Java 编写的:

I am slightly moving from Java to Kotlin in android app development, but there are some cases where I don't want to code in Kotlin and want those particular cases be written in Java:

  • Kotlin 删除了额外使用的findViewByIds
  • 我知道现在是 null-safety,而 Java 总是用 NullPointerException
  • 触发还提供了
  • Lambda 表达式和许多其他功能.
    • It is effort-saving that Kotlin removes extra usage of findViewByIds
    • I know that it is now null-safety which oppositely Java always triggered with NullPointerException
    • Lambda expressions are also provided and many other features.
    • 但是,我的一些代码仍然不能用 Kotlin 编写,例如 static 成员或 non-primitive 字段.

      But still, some of my codes cannot be written in Kotlin such as static members or non-primitive fields.

      Kotlin 注解实际上可以替代那些静态成员.但是喜欢 Java 的一些编码功能,不想离开.

      Kotlin annotations actually can replace those static members. But love some coding features of Java and dont want to leave that.

      我的问题是我可以将 Java 和 Kotlin 结合在一起吗?

      推荐答案

      如果您的问题是您能否在 java 文件中使用 kotlin 文件,反之亦然,那么答案是肯定的.

      If your question is can you use kotlin files in java files and vice versa then the answer is yes.

      如果您问是否可以在 java 文件中使用 kotlin 语法,反之亦然,那么答案是否定的.

      If you are asking if you can use kotlin syntax in java files and vice versa then the answer is no.

      要在 java 类中使用 kotlin 代码,您只需像使用任何其他 java 类一样使用该类

      To use kotlin code in a java class you are simply using the class like any other java class

      这篇关于在 Android 中结合 Java 和 Kotlin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆