Kotlin Annotation IntDef

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

提交回复
热议问题