将代码从 Java 更改为 Kotlin 后注释未正确转换

Annotations not converted properly after changing code from Java to Kotlin

将一些 Java 代码转换为 Kotlin(以创建 ReplacementSpan)后,返回了与注释相关的错误。

from@IntRange(from = 0)构造函数中returns报错:

'IntRange' is not an annotation class | Cannot find a parameter with this name: from

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 混淆了 Android SDK 自己的 IntRange class with the 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) {
}