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

查看:166
本文介绍了在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注释实际上可以替换那些static members.但是喜欢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天全站免登陆