Kotlin Annotation IntDef

前端 未结 6 1996
不知归路
不知归路 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:08

    Strange thing, but this question comes in search before the same with right answer

    Copying it here:

    import android.support.annotation.IntDef
    public class Test {
    
        companion object {
    
             @IntDef(SLOW, NORMAL, FAST)
             @Retention(AnnotationRetention.SOURCE)
             annotation class Speed
    
             const val SLOW = 0
             const val NORMAL = 1
             const val FAST = 2
        }
    
        @Speed
        private var speed: Int=SLOW
    
        public fun setSpeed(@Speed speed: Int) {
            this.speed = speed
        }
    }
    
    0 讨论(0)
  • 2020-12-03 10:11

    If you are calling setMeasureText from Java you can get it to work by creating your IntDef in Java too

    // UnitType.java
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({MeasureText.UNIT_KG, MeasureText.UNIT_LB, MeasureText.UNIT_NONE})
    public @interface UnitType {}
    

    h/t Tonic Artos

    You will also need to update your companion object to make your values longs and publicly accessible

    companion object{
        const val UNIT_NONE = -1L
        const val UNIT_KG = 1L
        const val UNIT_LB = 0L
    }
    
    0 讨论(0)
  • 2020-12-03 10:21

    Try it:

    companion object {
        const val VALUE_1: Int = 1
        const val VALUE_2: Int = 2
        const val VALUE_3: Int = 3
    }
    
    @IntDef(value = [VALUE_1, VALUE_2, VALUE_3])
    @Retention(AnnotationRetention.SOURCE)
    annotation class TestClassName
    
    0 讨论(0)
  • 2020-12-03 10:21

    As the accepted answer said,use an enum class in kotlin.

    I wrote the specific code of the question asked,it may help some people new in kotlin:

    class MeasureTextView: TextView {
       enum class UnitType(val value : Int){
           UNIT_NONE(-1),
           UNIT_KG(0),
           UNIT_LB(1)
       }
    
       fun setMeasureText(number: Float, unitType: UnitType){
           val suffix = unitType.value
       }
    }
    
    0 讨论(0)
  • 2020-12-03 10:23

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

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

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题