Kotlin Annotation IntDef

前端 未结 6 2006
不知归路
不知归路 2020-12-03 09:53

I have this code sample:

class MeasureTextView: TextView {
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: Attr         


        
6条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 10:28

    My preferred way to use IntDef with Kotlin is to use top-level declarations:

    package com.example.tips
    
    
    const val TIP_A = 1
    const val TIP_B = 2
    const val TIP_C = 3
    
    @IntDef(TIP_A, TIP_B, TIP_C)
    @Retention(AnnotationRetention.SOURCE)
    annotation class TipId
    
    
    class TipsDataProvider {
    
        fun markTip(@TipId tipId: Int) {
        ...
        }
    }
    

    No extra class or object required! More info about top-level declarations here.

提交回复
热议问题