如何将 Java 源文件的一部分转换为 Kotlin?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34588117/
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
How can I convert a part of Java source file to Kotlin?
提问by Alan Coromano
In my Kotlin project I have some parts of Java code which I want to convert to Kotlin. The menu item Convert Java file to Kotlin is disabled because it's not a whole file I want to convert.
在我的 Kotlin 项目中,我想将 Java 代码的某些部分转换为 Kotlin。菜单项 Convert Java file to Kotlin 被禁用,因为它不是我想要转换的整个文件。
How can I convert Java code to Kotlin?
如何将 Java 代码转换为 Kotlin?
采纳答案by yole
There is no tool to convert Kotlin code to 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.
没有将 Kotlin 代码转换为 Java 的工具。如果要将文件的一部分从 Java 转换为 Kotlin,最简单的方法是从 IDE 中的 Java 文件中复制代码并将其粘贴到 Kotlin 文件中。
回答by Ruslan
I found a workaround:
我找到了一个解决方法:
- Create a Java file and paste inside this file Java code, which you want covert.
- Now copy Java code from Java file.
- Go to Kotlin file and paste this code. Idea will suggest convert code from Java to Kotlin.
- 创建一个 Java 文件并在该文件中粘贴您想要隐藏的 Java 代码。
- 现在从 Java 文件复制 Java 代码。
- 转到 Kotlin 文件并粘贴此代码。Idea 会建议将代码从 Java 转换为 Kotlin。
回答by okarakose
With Android Studio 3.0
使用 Android Studio 3.0
You don't need to installany plugin to convert java code to kotlin code. Now, Google is officially supportingKotlin language.
您无需安装任何插件即可将 java 代码转换为 kotlin 代码。现在,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 toolunder 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.
- Android Studio Menu -> Code -> Convert Java File to Kotlin File
- 从项目结构中选择您的 java 类文件。
- Android Studio 菜单 ->代码 -> 将 Java 文件转换为 Kotlin 文件
回答by Benjamin
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. Here is a simple way to ensure that code is converted into Kotlin.
问题似乎源于 IntelliJ 不知道代码是 Java。这是确保代码转换为 Kotlin 的简单方法。
- Copy the Java code from your web browser.
- Open Scratch File (File -> New -> Scratch File or Ctrl+Alt+Shift+Insert)
- In the Languages dialog select Java as the language.
- 从 Web 浏览器复制 Java 代码。
- 打开临时文件(文件 -> 新建 -> 临时文件或 Ctrl+Alt+Shift+Insert)
- 在语言对话框中选择 Java 作为语言。
- Paste the previously copied Java snippet.
- Select All (Ctrl-A) and Copy (Ctrl-C)
- Now Paste into your Kotlin code.
- 粘贴之前复制的 Java 片段。
- 全选 (Ctrl-A) 和复制 (Ctrl-C)
- 现在粘贴到您的 Kotlin 代码中。
By first pasting the Java code into a Java scratch file, you have let IntelliJ know that the code snippet is Java code. It will then automatically convert the code to Kotlin when pasting into a Kotlin file.
通过首先将 Java 代码粘贴到 Java 暂存文件中,您已经让 IntelliJ 知道代码片段是 Java 代码。然后在粘贴到 Kotlin 文件时,它会自动将代码转换为 Kotlin。
回答by Benjamin
In Android 3.0 Canary, it is very simple: Go to Code; the last option is 'Convert Java file into Kotlin.' And you are done!
在 Android 3.0 Canary 中,非常简单:Go to Code;最后一个选项是“将 Java 文件转换为 Kotlin”。你已经完成了!
There is also a shortcut: Ctrl+Alt+Shift+K
还有一个快捷键:Ctrl+Alt+Shift+K
回答by Prasanth Yejje
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 代码文件...
回答by Jayakrishnan PM
In Android Studio 3.0
在 Android Studio 3.0 中
Kotlin is fully supported from Android Studio 3.0(i.e, NO Kotlin Plugin required). If you have Android Studio >= 3.0then your following jobs are very easy,
Android Studio 3.0完全支持 Kotlin (即不需要 Kotlin 插件)。如果您有 Android Studio >= 3.0,那么您的以下工作非常简单,
- You can create new Kotlin file.
- You can add Kotlin code to existing Project.
- You can convert entire JAVA code into Kotlin
- 您可以创建新的 Kotlin 文件。
- 您可以将 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 Yesto convert the code to Kotlin. You can check Don't show this dialog next time, which makes it easy to dump Java code snippets into your Kotlin files.
或者,创建一个新的 Kotlin 文件(文件 > 新建 > Kotlin 文件/类),然后将您的 Java 代码粘贴到该文件中——出现提示时,单击是将代码转换为 Kotlin。您可以选中下次不再显示此对话框,这样可以轻松地将 Java 代码片段转储到您的 Kotlin 文件中。
Dialog Window
对话窗口
Official Source :: Convert to Kotlin From JAVA
Note::When you create a new project
注意::当你创建一个新项目时
Tick checkbox Include Kotlin supportto 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 支持包含到整个项目的选项。
回答by Khyati Fatania
If your Android Studio version is less than 3 then you have to install plugin of Kotlin. To install it follow steps from below link
如果您的 Android Studio 版本低于 3,则必须安装 Kotlin 插件。要安装它,请按照以下链接中的步骤操作
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和查找动作(后Ctrl+ Shift+ A)和类型转换Java文件到科特林。
回答by Sanjay Goswami
Simple Ctrl+Alt+Shift+ K code will convert into kotlin
简单的 Ctrl+Alt+Shift+K 代码将转换为 kotlin
回答by airowe
From kotlinlang.org, click on "Try Online" at the top of the page. You'll see an option to "Convert from Java" in the toolbar.
从 kotlinlang.org,单击页面顶部的“在线试用”。您将在工具栏中看到“从 Java 转换”的选项。