将一些Java代码转换为Kotlin(以创建ReplacementSpan)后,将返回与注释相关的错误。from
构造函数中的@IntRange(from = 0)
返回错误:
import android.graphics.Canvas
import android.graphics.Paint
import sun.swing.SwingUtilities2.drawRect
import android.text.style.ReplacementSpan
class HrSpan : ReplacementSpan() {
override fun getSize(
paint: Paint, text: CharSequence, @IntRange(from = 0) start: Int,
@IntRange(from = 0) end: Int, fm: Paint.FontMetricsInt?
): Int {
return 0
}
override fun draw(
canvas: Canvas, text: CharSequence, @IntRange(from = 0) start: Int,
@IntRange(from = 0) end: Int, x: Float, top: Int, y: Int, bottom: Int,
paint: Paint
) {
canvas.drawRect(x, top.toFloat(), y.toFloat(), (top + 8).toFloat(), paint)
}
}
最佳答案
Kotlin将自己的 IntRange
类与Android SDK的 IntRange
注释混淆了。
像这样输入一个名字:
对于Android支持库:
import android.support.annotation.IntRange as AndroidIntRange
对于AndroidX:
import androidx.annotation.IntRange as AndroidIntRange
并像这样使用它:
fun foo(@AndroidIntRange(from = 0, to = 255) bar: Int) {
}