我有这个代码示例:
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(...)
,
静态变量显示为该方法参数的选项。
我对此进行了搜索,但我找不到 Kotlin 是否支持这种 java 风格的注释(例如 intdef)。所以我尝试过,并为此做了注释,但它不会显示在自动完成中。
我的问题:
- Kotlin(最新版本)是否支持 Java 注解 IntDef
奇怪的是,这个问题在搜索之前就出现了正确答案 https://stackoverflow.com/a/38537175/5593959
复制到这里:
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
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)