在 Android 中结合 Java 和 Kotlin

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46284545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:09:00  来源:igfitidea点击:

Combine Java with Kotlin in Android

javaandroidkotlin

提问by Zafar Kurbonov

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:

我在 android 应用程序开发中稍微从 Java 转向 Kotlin,但在某些情况下,我不想在 Kotlin 中编写代码并希望用 Java 编写这些特定情况:

  • It is effort-saving that Kotlin removes extra usage of findViewByIds
  • I know that it is now null-safetywhich oppositely Java always triggered with NullPointerException
  • Lambdaexpressions are also provided and many other features.
  • Kotlin 删除了额外的使用是省力的 findViewByIds
  • 我知道它现在null-safety与 Java 总是以相反的方式触发NullPointerException
  • Lambda还提供了表达式和许多其他功能。

But still, some of my codes cannot be written in Kotlin such as staticmembers or non-primitivefields.

但是,我的一些代码不能用 Kotlin 编写,例如static成员或non-primitive字段。

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

Kotlin 注释实际上可以替代那些static members. 但是喜欢 Java 的一些编码功能,不想离开它。

My question is can I combine Java and Kotlin together?

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

回答by tyczj

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 语法,反之亦然,那么答案是否定的。

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

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

回答by Bhuvanesh BS

You can create a java similar staticvariables using companion object.

您可以使用伴随对象创建一个类似于 java 的静态变量。

You can easily create singletonin Kotlin using object.

您可以使用object在 Kotlin 中轻松创建单例

After I leant kotlin. I have migrated all my codes to Kotlin.

在我学习了 kotlin 之后。我已将所有代码迁移到 Kotlin。

回答by Leonardo Cavazzani

You can have in your project Java and Kotlin Class and Activities.

您可以在您的项目中使用 Java 和 Kotlin 类和活动。

The same Class can't have Java and Kotlin. You can convert a Java file to Kotlin with Ctrl+Alt+Shift+Kor just double tap SHIFT and search for Convert Java to Kotlin

同一个 Class 不能有 Java 和 Kotlin。您可以使用Ctrl+Alt+Shift+K将 Java 文件转换为 Kotlin,或者只需双击 SHIFT 并搜索Convert Java to Kotlin

回答by Emanuel S

Even if it's interop you can't mix Java and Kotlin in the same file.

即使它是互操作的,你也不能在同一个文件中混合 Java 和 Kotlin。

If you really want to have static methods/variables you can use an companion object. You can also access create a "real" static method in your JVM by using @JvmStatic.

如果你真的想要静态方法/变量,你可以使用companion object. 您还可以使用@JvmStatic.

Simple sample is:

简单的例子是:

companion object {  
    @JvmStatic 
    fun newInstance() ) SampleFragment()
}

which equals to

这等于

public static SampleFragment() { return new SampleFragment(); } 

By using @JvmStaticyou can use Java to access your Static methods like before.

通过使用,@JvmStatic您可以像以前一样使用 Java 访问您的静态方法。