简体   繁体   English

如何将 Java 源文件的一部分转换为 Kotlin?

[英]How can I convert a part of Java source file to Kotlin?

In my Kotlin project, I have some parts of Java code that I want to convert to Kotlin.在我的 Kotlin 项目中,我想将 Java 代码的某些部分转换为 Kotlin。 The menu item that converts the Java file to Kotlin is disabled because it's not a whole file I want to convert.将 Java 文件转换为 Kotlin 的菜单项被禁用,因为它不是我想要转换的整个文件。

How can I convert Java code to Kotlin?如何将 Java 代码转换为 Kotlin?

With Android Studio 3.0使用 Android Studio 3.0

You don't need to install any plugin to convert java code to kotlin code.无需安装任何插件即可将 java 代码转换为 kotlin 代码。 Now, Google is officially supporting Kotlin language.现在,Google正式支持Kotlin 语言。

  • Android Studio Menu -> Code -> Convert Java File to Kotlin File Android Studio 菜单 ->代码 -> 将 Java 文件转换为 Kotlin 文件

Before Android Studio 3.0 Android Studio 3.0 之前

If you are using older Android Studio ( which is below version 3.0 ) there is a Kotlin tool under the menu.如果您使用的是旧版 Android Studio(低于 3.0 版),则菜单下有一个Kotlin 工具

Installing Kotlin plugin :安装 Kotlin 插件:

  • Android Studio Menu -> File -> Settings -> Plugins -> Browse repositories -> Kotlin Android Studio 菜单 ->文件 -> 设置 -> 插件 -> 浏览存储库 -> Kotlin

Converting Java Code to Kotlin Code :将 Java 代码转换为 Kotlin 代码:

  • Select your java class file from project structure.从项目结构中选择您的 java 类文件。
  • Android Studio Menu -> Code -> Convert Java File to Kotlin File Android Studio 菜单 ->代码 -> 将 Java 文件转换为 Kotlin 文件

There is no tool to convert Kotlin code to Java.没有将 Kotlin 代码转换为 Java 的工具。 If you want to convert part of a file from Java to Kotlin, the easiest way is to copy the code from the Java file in the IDE and paste it into the Kotlin file.如果要将文件的一部分从 Java 转换为 Kotlin,最简单的方法是从 IDE 中的 Java 文件中复制代码并将其粘贴到 Kotlin 文件中。

To convert Java code in Android studio use CTRL+SHIFT+ALT+K要在 Android Studio 中转换 Java 代码,请使用 CTRL+SHIFT+ALT+K

To convert from Website use Try Kotlin site.要从网站转换,使用尝试 Kotlin网站。

To convert from Android app use Master in Android .要从 Android 应用程序转换,请使用Android 中的 Master

I found a workaround:我找到了一个解决方法:

  1. Create a Java file and paste inside this file Java code, which you want to convert.创建一个 Java 文件并将 Java 代码粘贴到该文件中,您要转换该代码。
  2. Now copy Java code from Java file.现在从 Java 文件复制 Java 代码。
  3. Go to the Kotlin file and paste this code.转到 Kotlin 文件并粘贴此代码。 IDEA will suggest convert code from Java to Kotlin. IDEA 会建议将代码从 Java 转换为 Kotlin。

To convert a complete file into Kotlin, use this combo of keys:要将完整文件转换为 Kotlin,请使用以下键组合:

  • Shift + Alt + Cmd + k in Mac Mac 中的Shift + Alt + Cmd + k
  • Shift + Alt + Ctrl + k in Windows Windows 中的Shift + Alt + Ctrl + k

In Android Studio 3.0在 Android Studio 3.0 中

Kotlin is fully supported from Android Studio 3.0 (ie, NO Kotlin Plugin required). Android Studio 3.0完全支持 Kotlin(即不需要 Kotlin 插件)。 If you have Android Studio >= 3.0 then your following jobs are very easy,如果您有 Android Studio >= 3.0,那么您的以下工作非常简单,

  • You can create new Kotlin file.您可以创建新的 Kotlin 文件。
  • You can add Kotlin code to existing Project.您可以将 Kotlin 代码添加到现有项目中。
  • You can convert entire JAVA code into Kotlin您可以将整个 JAVA 代码转换为 Kotlin

Convert existing Java code to Kotlin code将现有的 Java 代码转换为 Kotlin 代码

In Android Studio 3.0, open a Java file and select Code > Convert Java File to Kotlin File.在 Android Studio 3.0 中,打开一个 Java 文件并选择代码 > 将 Java 文件转换为 Kotlin 文件。

Or, create a new Kotlin file (File > New > Kotlin File/Class) , and then paste your Java code into that file—when prompted, click Yes to convert the code to Kotlin.或者,创建一个新的 Kotlin 文件(文件 > 新建 > Kotlin 文件/类) ,然后将您的 Java 代码粘贴到该文件中——出现提示时,单击将代码转换为 Kotlin。 You can check Don't show this dialog next time , which makes it easy to dump Java code snippets into your Kotlin files.您可以选中下次不再显示此对话框,这样可以轻松地将 Java 代码片段转储到您的 Kotlin 文件中。

Dialog Window对话窗口

在此处输入图片说明

Official Source :: Convert to Kotlin From JAVA 官方来源 :: 从 JAVA 转换为 Kotlin

Note::When you create a new project注意::当你创建一个新项目时

Tick checkbox Include Kotlin support to get kotlin support for the entire project.勾选复选框Include Kotlin support以获得整个项目的 kotlin 支持。

The below is the screen shot of first screen of new Android Project Creation Window with an option to include Kotlin Support to the entire project.下面是新的 Android 项目创建窗口的第一个屏幕的屏幕截图,其中包含将 Kotlin 支持包含到整个项目的选项。

使用 Kotlin 支持创建新的 Android 项目

简单的 Ctrl+Alt+Shift+K 代码将转换为 kotlin

When pasting Java code into Kotlin file, using IntelliJ derived IDE, the IDE will not convert the Java into Kotlin.将 Java 代码粘贴到 Kotlin 文件时,使用 IntelliJ 派生的 IDE,IDE 不会将 Java 转换为 Kotlin。

The problem seems to stem from IntelliJ not knowing the code is Java.问题似乎源于 IntelliJ 不知道代码是 Java。 Here is a simple way to ensure that code is converted into Kotlin.这是确保代码转换为 Kotlin 的简单方法。

  1. Copy the Java code from your web browser.从 Web 浏览器复制 Java 代码。
  2. Open Scratch File (File -> New -> Scratch File or Ctrl+Alt+Shift+Insert)打开临时文件(文件 -> 新建 -> 临时文件或 Ctrl+Alt+Shift+Insert)
  3. In the Languages dialog select Java as the language.在语言对话框中选择 Java 作为语言。

新的 Scratch 窗口语言对话框。

  1. Paste the previously copied Java snippet.粘贴之前复制的 Java 片段。
  2. Select All (Ctrl-A) and Copy (Ctrl-C)全选 (Ctrl-A) 和复制 (Ctrl-C)
  3. Now Paste into your Kotlin code.现在粘贴到您的 Kotlin 代码中。

By first pasting the Java code into a Java scratch file, you have let IntelliJ know that the code snippet is Java code.通过首先将 Java 代码粘贴到 Java 暂存文件中,您已经让 IntelliJ 知道代码片段是 Java 代码。 It will then automatically convert the code to Kotlin when pasting into a Kotlin file.然后在粘贴到 Kotlin 文件时,它会自动将代码转换为 Kotlin。

click here for image单击此处查看图像

In Android 3.0 Canary, it is very simple: Go to Code;在 Android 3.0 Canary 中,非常简单:Go to Code; the last option is 'Convert Java file into Kotlin.'最后一个选项是“将 Java 文件转换为 Kotlin”。 And you are done!你已经完成了!

There is also a shortcut: Ctrl+Alt+Shift+K还有一个快捷键:Ctrl+Alt+Shift+K

its a simple process.. go into your Android studio's tool bar and Do follow this Step这是一个简单的过程..进入你的Android工作室的工具栏,然后按照这个步骤

CODE -> Convert java file to kotlin file..代码 -> 将 java 文件转换为 kotlin 文件..

then it asks you for Confirmation and Restart the Android Studio IDE..when you confirm then it restart your IDE and it show you converted Kotlin code file...然后它会要求您确认并重新启动 Android Studio IDE..当您确认时,它会重新启动您的 IDE 并显示您转换后的 Kotlin 代码文件...

You don't need any specific tools.您不需要任何特定的工具。 Just simply copy and paste from anywhere and paste to .kt file in Android Studio.只需简单地从任何地方复制和粘贴,然后粘贴到Android Studio. .kt文件即可Android Studio. Dialog Window will be open by asking "Convert Java to Kotlin ".对话框窗口将通过询问“Convert Java to Kotlin ”打开。 Press Yes, It will convert it and do some miner changes.按是,它将转换它并进行一些矿工更改。

If your Android Studio version is less than 3 then you have to install plugin of Kotlin.如果您的 Android Studio 版本低于 3,则必须安装 Kotlin 插件。 To install it follow steps from below link要安装它,请按照以下链接中的步骤操作

Blog jetbrains :- Working with Kotlin in Android Studio 博客 jetbrains :- 在 Android Studio 中使用 Kotlin

After installing plugin restart Android Studio and Find Action ( Ctrl + Shift + A ) and type Convert Java File to Kotlin.安装插件后重启 Android Studio 和 Find Action ( Ctrl + Shift + A ) 并输入 Convert Java File to Kotlin。

From kotlinlang.org, click on "Try Online" at the top of the page .从 kotlinlang.org,单击页面顶部的“在线试用” You'll see an option to "Convert from Java" in the toolbar.您将在工具栏中看到“从 Java 转换”的选项。

点击在线试用

单击从 Java 转换

The simplest way to do this -最简单的方法来做到这一点 -

  1. Copy the code from the .java file.从 .java 文件复制代码。
  2. Create a new file with .kt extension.创建一个扩展名为 .kt 的新文件。
  3. Paste the code.粘贴代码。
  4. A pop up would appear asking to convert the code you pasted to Kotlin.将出现一个弹出窗口,要求将您粘贴的代码转换为 Kotlin。 Allow.允许。

In the new Android Studio version of 4.1 and above, you don't have to do any of the above stuff.在 4.1 及以上的新 Android Studio 版本中,您无需执行上述任何操作。 You simply copy and paste any Java code into any existing kt file in Android Studio and it will recognize it as Java code needed to be converted to Kotlin.您只需将任何 Java 代码复制并粘贴到 Android Studio 中的任何现有 kt 文件中,它就会将其识别为需要转换为 Kotlin 的 Java 代码。 Then it will ask you in a dialog box whether it's something you want to do and convert it right in the spot if you answer yes.然后它会在对话框中询问您是否要执行此操作,如果您回答是,则立即将其转换。 You can also check the "do not ask to convert" and it will automatically convert any Java code you paste in the future for you.您还可以选中“不要求转换”,它会自动转换您将来粘贴的任何 Java 代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM