Kotlin注释说明 [英] Kotlin Annotation IntDef

查看:415
本文介绍了Kotlin注释说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码示例:

class MeasureTextView: TextView {
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)

    companion object{
        val UNIT_NONE = -1
        val UNIT_KG = 1
        val UNIT_LB = 0            
    }

    fun setMeasureText(number: Float, unitType: Int){

        val suffix = when(unitType){
            UNIT_NONE -> {
                EMPTY_STRING
            }
            UNIT_KG -> {
                KG_SUFIX
            }
            UNIT_LB -> {
                LB_SUFIX
            }
            else -> throw IllegalArgumentException("Wrong unitType passed to formatter: MeasureTextView.setMeasureText")
        }

        // set the final text
        text = "$number $suffix"
    }
}

我希望能够在编译时与IntDef注释一起使用自动完成功能,因此当我调用setMeasureText(...)时, 静态变量显示为该方法参数的选项.

I want to be able to use, at compile time, the auto complete feature in conjunction with IntDef annotation, so when i invoke setMeasureText(...), the static variables are shown as options to the argument of this method.

我已经搜索过有关此内容,但是我找不到Kotlin是否支持此Java样式的注释(例如,intdef).所以我已经尝试过了,并为此做了一个注释,但是它不会自动完成显示.

I have searched about this, and i couldn't find if Kotlin supported this java-style annotations (intdef for example). So i have tried it, and made an annotation for this, but it won't show in autocompletion.

我的问题: -Kotlin(最新版本)是否支持Java注释IntDef

My question: - Is Java annotation IntDef supported in Kotlin (latest version)

  • 如果是的话,我该如何在Android Studio IDE中将其打开(如果它可以正常工作,我将无法让编译器提出建议).

  • If it is, how can i turn in ON in the Android Studio IDE (if it works, i can't get the compiler to suggest it).

如果不是,是否有任何Kotlin方式可以进行编译时间检查

If it is not, is there any Kotlin-way of make this compile time checks

推荐答案

从Kotlin 1.0.3开始,不支持@IntDef批注,但计划在更高版本中提供支持.

As of Kotlin 1.0.3, the @IntDef annotation is not supported, but support is planned for later versions.

进行这些编译时检查的Kotlin方法是使用enum class而不是一系列的Int常量.

The Kotlin way of making these compile time checks is to use an enum class instead of a series of Int constants.

这篇关于Kotlin注释说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆