简体   繁体   English

如何使用 Android Studio 为 newInstance Fragments 创建 kotlin 实时模板

[英]How to create a kotlin live template for newInstance Fragments using Android studio

I'm searching for a way to create a new Kotlin live code template so that whenever I type newIns.... it and hit tab it will be able to print the following as a live template choice:我正在寻找一种方法来创建一个新的 Kotlin 实时代码模板,这样每当我输入 newIns.... 它并点击选项卡时,它将能够打印以下内容作为实时模板选择:

companion object {
    fun newInstance(b: Bundle): DetailsFragment {
        val frag = DetailsFragment()
        frag.arguments = b
        return frag
    }
}

In Java, it was done the same way and there is already a "newInstance" abbreviation and a live template exists in Android Studio.在 Java 中,它是以相同的方式完成的,并且已经有一个“newInstance”的缩写,并且在 Android Studio 中存在一个实时模板。 I want the same thing for Kotlin.我想对 Kotlin 做同样的事情。 Let me show you a photo :让我给你看一张照片:

在此处输入图片说明

Notice that, Java Android already has newInstance template.请注意,Java Android 已经有newInstance模板。 I want this for Kotlin.我想要这个用于 Kotlin。 Here is what I have so far:这是我到目前为止所拥有的:

在此处输入图片说明

and the template code I have so far looks like this:到目前为止,我的模板代码如下所示:

companion object { 
    fun newInstance($args$:Bundle):$fragment$ {
        $nullChecks$
        android.os.Bundle args = Bundle();
        $addArgs$
        $fragment$ fragment = $fragment$();
        fragment.setArguments(args);
        return fragment;
    }
}

but when I exit the settings and in Kotlin type the first few words of the abbreviation and hit tab or ctrl + spacebar on mac nothing happens.但是当我退出设置并在 Kotlin 中输入缩写的前几个词并在 Mac 上点击 Tab 或 ctrl + 空格键时没有任何反应。 I guess I have the syntax wrong, I'm not sure.我想我的语法有误,我不确定。 Anyone of suggestions?任何人的建议?

Step 1: 步骤1:

Go to Live Templates section in Android Studio. 转到Android Studio中的“实时模板”部分。

For Windows: 对于Windows:

File > Settings > Editor > Live Templates 文件 > 设置 > 编辑器 > 实时模板

For Mac: 对于Mac:

Android Studio > Preferences > Editor > Live Templates Android Studio > 首选项 > 编辑器 > 实时模板

Step 2: 第2步:

Select Kotlin template group. 选择Kotlin模板组。 Then tap on + present at the top-right corner of the popup. 然后点击弹出窗口右上角的+ present。 Select Live Template . 选择实时模板

Step 3: 第3步:

Now you can add your live template. 现在您可以添加实时模板了。 Check at the bottom of the popup. 检查弹出窗口的底部。

Add abbreviation: newInstance 添加缩写: newInstance

Add description: Creates an instance of the fragment with arguments 添加描述:使用参数创建片段的实例

Add template text: 添加模板文字:

companion object {
    fun newInstance(args: Bundle): $fragment$ {
        val fragment = $fragment$()
        fragment.arguments = args
        return fragment
    }
}

Add applicable context . 添加适用的上下文 Tap on Define . 点击“ Define Select Kotlin from the list. 从列表中选择Kotlin。

Select Reformat according to style 根据样式选择重新格式化

Step 4: 第4步:

Tap on Edit Variables below the description. 点击描述下方的编辑变量

Now tap on Expression for the variable name fragment . 现在点击Expression来获取变量名称fragment Tap on down arrow. 点击向下箭头。 You can see a list of expressions. 您可以看到表达式列表。 From there select kotlinClassName() . 从那里选择kotlinClassName()

Tap on OK of the Edit Templates Variable 点击编辑模板变量的确定

Now tap on Apply and OK of the Live Templates. 现在点击“实施模板”的“ 应用”和“ 确定 ”。

To check type newInstance in a Fragment written in Kotlin. 检查在Kotlin中编写的片段中的newInstance类型。

A slightly simplified version (and more idiomatic) would be:稍微简化的版本(更惯用)是:

fun newInstance($args$) : $fragment$ = 
    $fragment$().apply {
        arguments = Bundle().apply {
            $addArgs$
        }
    }

or或者

fun newInstance(args: Bundle) : $fragment$ = 
    $fragment$().apply {
        arguments = args
    }

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

相关问题 使用 NavController 时如何使用 newInstance() 创建片段? - How to create fragments with newInstance() when using NavController? 如何使用Android中的片段创建可滚动模板 - How to create a scrollable template using fragments in Android Kotlin-如何将空值传递给片段newInstance方法? - Kotlin - how to pass in null value into fragments newInstance method? 如何使用 Kotlin 和 Android Studio 在动态壁纸中声明和设置按钮 - How to declare and set buttons in a live wallpaper using Kotlin and Android Studio 如何使用 kotlin 在 Android Studio 中创建按钮 - How to create a button in Android Studio using kotlin Android Studio:如何使用 fbc 直播模板 - Android Studio: how to use the fbc live template 使用Kotlin的@JvmOverloads和Android的Fragment.newInstance()模式 - Using Kotlin's @JvmOverloads with Android's Fragment.newInstance() pattern 如何为具有各种片段的 Kotlin Android 应用程序创建带协程的单例计时器? - How to create a Singleton timer with a coroutine for a Kotlin Android app with various fragments? 如何使用 MVVM 从 android studio Kotlin 中的谷歌地图标记的 api 调用实时数据 - How to call live data from a api for google maps marker in android studio Kotlin using MVVM 在片段 Kotlin Android Studio 中描述此问题 - Context this problem in fragments Kotlin Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM